Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple ID's in a badly designed webpage?

I am maintaining a complex web application.

I have a large number of divs which all have the same ID.

I know this is totally wrong, and as a matter of fact document.getElementById() with that id is going to only produce one match for me.

However I am able to pull out the element that I'm looking for using jQuery (we are on 1.6.2), like this: $('#bad_id[nonstandard_attr_name=somethingSpecific]')

Not quite ready to say that this is a "solution".

I'm worried about whether this is reliable or not. Is jQuery really actually gonna search through all the elements that match the ID using a DOM walk? That's probably the only way to get all of them.

Does it filter elements by the other attribute first, and then filter it down by the ID? That would achieve the desired behavior as well, but it would be good to know the order it does this in.

like image 960
Steven Lu Avatar asked Feb 15 '23 15:02

Steven Lu


1 Answers

If you need to select multiple elements with same id you can simply use an attribute selector:

$( "[id='myid']" )

The attribute selector doesn't look at the attribute key for any semantics like unique ids or such.

http://jsfiddle.net/ZWm3G/

like image 183
Esailija Avatar answered Feb 18 '23 11:02

Esailija