Reputation: 10484
What will jQuery("div#someid")
return if the html is invalid and has many div elements with the same "someid". Will it return the first of those divs ? Or is that random ?
Upvotes: 0
Views: 123
Reputation: 50603
The best to know that is try it out, see it in action in this fiddle http://tinker.io/cba17 , so the answer is it will return a set with all the divs that have that id. Although please avoid that situation because it is invalid html and use a class instead.
Also notice there's a difference between using $('#someid'
) and $('div#someid')
:
$('#someid'
) will return the first div with that id.
$('div#someid')
will return the complete set with all elements as showed in the fiddle I posted above.
Upvotes: 2
Reputation: 700562
The selector will give different results in different browsers.
Some testing shows these results:
Firefox 19: All elements
IE 10: All elements
IE 9: All elements
IE 8: All elements
IE 7: One element
Chrome 25: All elements
The result might also vary with other factors, like jQuery version, page rendering mode and operating system. You simply can't expect a consistent result with conflicting id attributes.
Upvotes: 6
Reputation: 36541
having same id is invalid... so you should avoid that... and use class...(though no error is thrown) anyways the selector will return first occurance of that div having same id..
Upvotes: 0