44

I have the following HTML:

<form name="frmSave">...</form>

Just to know, I am not able to modify the HTML in order to add an id or something else.

This is what I tried to get the form element by it's name:

var frm = $('form[name="frmSave"]');

console.log(frm);

(but I believe the above code will try to get a children element with the frmSave name inside the form which is not what I need).

How can I achieve this, is it possible to get the form by just the name and using a selector?


Update:

I was doing the wrong way in my original code (I was using a space aka "combinator" in the selector) and not in the code snippet from here so jQuery was trying to get any children element with the name needed and I was asking myself what was wrong since nothing was returned.

The accepted answer gives a better explanation about this so a little space could change everything - I will sleep properly next time :-)

2
  • Are you sure there are no other JS errors on the page stopping this from executing? Check the console logs.
    – webnoob
    Dec 6, 2012 at 17:02
  • 1
    I guess it was correct but for some reason it prints null at console, that's why I asked, maybe other thing could be the problem but I will check everything again.
    – Oscar Jara
    Dec 6, 2012 at 17:06

4 Answers 4

83

$('form[name="frmSave"]') is correct. You mentioned you thought this would get all children with the name frmsave inside the form; this would only happen if there was a space or other combinator between the form and the selector, eg: $('form [name="frmSave"]');

$('form[name="frmSave"]') literally means find all forms with the name frmSave, because there is no combinator involved.

0
11
    // this will give all the forms on the page.

    $('form')

   // If you know the name of form then.

    $('form[name="myFormName"]')

  //  If you don't know know the name but the position (starts with 0)

    $('form:eq(1)')  // 2nd form will be fetched.
2
  • I just had one form on the page and i use $('form') and it worked
    – Atta H.
    Aug 8, 2018 at 11:07
  • it will return all the form, and there is only one form. Aug 15, 2018 at 16:34
6

You have no combinator (space, >, +...) so no children will get involved, ever.

However, you could avoid the need for jQuery by using an ID and getElementById, or you could use the old getElementsByName("frmSave")[0] or the even older document.forms['frmSave']. jQuery is unnecessary here.

3
  • document.querySelector("form[name='frmSave']") would work too, but only in newer browsers. Dec 6, 2012 at 17:04
  • I know it is unnecessary but I am working with other code that requires jQuery that's why I am using it for this, thanks for your explanation btw +1.
    – Oscar Jara
    Dec 6, 2012 at 17:09
  • Nothing "requires" jQuery, not really ;) I'm happily using accordions, sliders, tooltips and modal dialogs with hand-written vanilla JS, and I feel much more accomplished for it. Dec 6, 2012 at 17:24
2

For detecting if the form is present, I'm using

if($('form[name="frmSave"]').length > 0) {
    //do something
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.