246

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)

1
  • If you just want to add fields and you already know what they should contain, you could just use hidden fields. Commented Sep 2, 2021 at 20:20

5 Answers 5

521

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");
Sign up to request clarification or add additional context in comments.

4 Comments

May be we should add getting by index? '$("input[type=submit]").closest("form");' returns an array of forms.
I'm trying to use the above in this way: $(".each img").click(function() { $(this).closest("form").show(); }); But I can't seem to get it to work. :/
peterjwest answer is better than this one.
In HTML5, there is a new 'form' attribute which allows you to have the element outside the parent form. This should be checked first.
58

You can use the form reference which exists on all inputs, this is much faster than .closest() (5-10 times faster in Chrome and IE8). Works on IE6 & 7 too.

var input = $('input[type=submit]');
var form = input.length > 0 ? $(input[0].form) : $();

5 Comments

You mention IE8. Does this work for versions 6, 7, and 9 as well?
This is much better and faster as @peterjwest mentioned. Re IE6 I think .form on input elements was on IE4, unfortunately netscape dev page is gone now... and who would check mozilla one.
This is a much safer way rather than using closest() since an input may have an own form assignment: codepen.io/anon/pen/vNqEyg
You probably used the shorthand if notation to keep your code short, but in this example it only distracts from the actual code you want to show (especially for people unfamiliar with the shorthand). I would be in favor of using a regular if().
I'd say there is too much clutter with that solution. If you go with this (my solution below - yes this is an advertisement :)): stackoverflow.com/a/18921404/261332, then you don't need that kind of complications, since it will just work when it's supposed to and do nothing otherwise.
22

To me, this looks like the simplest/fastest:

$('form input[type=submit]').click(function() { // attach the listener to your button
   var yourWantedObjectIsHere = $(this.form);   // use the native JS object with `this`
});

1 Comment

For me, using $(this.form) is the best solution
3

As of HTML5 browsers one can use inputElement.form - the value of the attribute must be an id of a <form> element in the same document. More info on MDN.

Comments

0

see also jquery/js -- How do I select the parent form based on which submit button is clicked?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.