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 :-)
$('form[name="frmSave"]')
is correct. You mentioned you thought this would get all children with the namefrmsave
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 namefrmSave
, because there is no combinator involved.You have no combinator (space,
>
,+
...) so no children will get involved, ever.However, you could avoid the need for jQuery by using an
ID
andgetElementById
, or you could use the oldgetElementsByName("frmSave")[0]
or the even olderdocument.forms['frmSave']
. jQuery is unnecessary here.For detecting if the form is present, I'm using