I have some code doing this :
var changes = document.getElementsByName(from); for (var c=0; c<changes.length; c++) { var ch = changes[c]; var current = new String(ch.innerHTML); etc. }
This works fine in FF and Chrome but not in IE7. Presumably because getElementsByName isn’t working in IE. What’s the best workaround?
In case you don’t know why this isn’t working in IE, here is the MSDN documentation on that function:
Firefox allows
getElementsByName()to retrieve elements that use a NAME expando, which is why it works. Whether or not that is a Good Thing™ may be up for debate, but that is the reality of it.So, one option is to use the
getAttribute()DOM method to ask for the NAME attribute and then test the value to see if it is what you want, and if so, add it to an array. This would require, however, that you iterate over all of the nodes in the page or at least within a subsection, which wouldn’t be the most efficient. You could constrain that list beforehand by using something likegetElementsByTagName()perhaps.Another way to do this, if you are in control of the HTML of the page, is to give all of the elements of interest an Id that varies only by number, e.g.:
And then have JavaScript like this:
That is a hack, of course, but it would do the job you need and not be too inefficient compare to some other hacks.
If you are using a JavaScript framework/toolkit of some kind, you options are much better, but I don’t have time to get into those specifics unless you indicate you are using one. Personally, I don’t know how people live without one, they save so much time, effort and frustration that you can’t afford not to use one.