Duplicate ID/indexes and looping

Posted by Justin Alexander on Stack Overflow See other posts from Stack Overflow or by Justin Alexander
Published on 2010-04-26T09:18:50Z Indexed on 2010/04/26 9:23 UTC
Read the original article Hit count: 222

Filed under:
|
|

I realize having two elements in the same html doc with the same ID is wrong, bad, immoral, and will lead to global warming. But... I'm trying to write an XSS widgit, so I really have no control over the quality of the parent web page.

I loop through document.images to retrieve a list of images on the page. I perform an action on each one.

for(img in document.images){
   ...
}

i've also tried

for(var i=0;i<document.images.length;i++){
...
}

in both cases it allows me to loop through all of the elements, BUT when trying trying to reference an object with a duplicate ID, I always get the first (in order of the html).

When using debugger in IE8 i'm able to see that both elements ARE listed, but that they both have the same index (in IE the index of the document.images is either sequential or matches the image ID)

Does anyone have a better solution?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about internet-explorer