tags:

views:

35

answers:

1

I post via Ajax and get a return value which I then want to insert into an input text field - NOte there are "many" similar forms on the page hence the use of .parent. What is the right JQuery statement please.

Thanks in advance

$j(this).parent('form input[name$="tesitID"]').val(data)
+1  A: 

The following jQuery should traverse from a trigger inside the form, to the form parent, then find the input and set the value to the supplied data.

$(this).parent('form').find('input[name="testID"]').val(data);

example:

<html>
<head>
<script type="text/javascript" src="jquery.js"> </script>
<script type="text/javascript">
$(function(){ 
  $("#trigger").click(
    function(){
     $(this).parent('form').find('input[name="testID"]').val('data');
    });
});
</script>
</head>
<body>

<form>
  <input type="text" value="" name='testID' />
  <input type="button" value="Load AJAX" id="trigger" />
</form>
</body>
</html>
phoenixillusion
To the OP, you might want to consider using `closest()` as it will allow you to retrieve the `<form>` element even if it's not the parent (i.e. if it's an ancestor).
J-P