What if I have 2 forms on 1 page:
<form id="form1">
<input type="text" />
<input type="text" />
</form>
<form id="form2">
<input type="text" />
</form>
And I want to access the input elements of the first one. I do the following:
$(document).ready(function() {
var bla = $('#form1:input');
alert(bla.length);
});
But it displays 0 count. I’ll appreciate if somebody can advise.
Thank you. The following syntax worked for me:
$('#form1 :input')
that is, putting the space.
Add a space to your selector.
#form1:inputmeans#form1is an:input#form1 :inputgrabs an:inputwithin#form1When you combine selectors without a space or comma, you’re saying “not only does it qualify as X, but as Y and Z. e.g.:
The above means it’s a
<div>element, that also has thefooclass and has an ID ofbar. e.g.<div class="foo" id="bar"></div>However:
The above means to look for a div that contains another element with the class
foowhich also contains another element with the class ofbar. e.g.<div><p class="foo"><img class="bar" /></p></div>