views:

2707

answers:

2

i have this html

<ul>
    <li><form action="#" name="formName"></li>
    <li><input type="text" name="someName" /></li>
    <li><input type="text" name="someOtherName" /></li>
    <li><input type="submit" name="submitButton" value="send"></li>
    <li></form></li>
</ul>

How can i select the form that the input[name="submitButton"] is part of ? (when i click on the submit button i want to select the form and append some fields in it)

+13  A: 

I would suggest using closest, which selects the closest matching parent element:

$('input[name="submitButton"]').closest("form");

Instead of filtering by the name, I would do this:

$('input[type=submit]').closest("form");
karim79
thanks karim ;)
kmunky
A: 

see also http://stackoverflow.com/questions/311579/jquery-js-how-do-i-select-the-parent-form-based-on-which-submit-button-is-clic

$('form#myform1').submit(function(e){
     e.preventDefault(); //Prevent the normal submission action
     var form = this;
     // ... Handle form submission
});
Jonathan Hendler