Discussions
Categories
Groups
Advertisement
Child Item
Home
Topics
Technology & Internet
Software & Web Development
Design
javascript document.getElementById(array item)
grahamor
Hi,
I have an array like so
var countries = new Array("us","uk","ie");
the us,uk,ie etc in the array are actually id's on divs on the page.
i want to reference these elements like so.
for(i=0;i<countries.length;i++)
{
document.getElememtById(countries
).className = "new class";
}
it's working in Firefox but in IE i am getting null errors.
How can countries
be interpreted as a div element ?
Thanks for any info
Find more posts tagged with
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
Advertisement
Advertisement
Comments
Eoin
That works for me in IE 7. You have a typo in "getElememtById", but I am guessing that's not in your original code or it wouldn't work in Firefox.
Eoin
So, did you get sorted?
grahamor
sorry, yeah i got sorted, turns out some of the elements i was looping through were not on the page (it varies because it's dynamic), so IE was throwing an error when it didnt find one of the elements on the page.
Thanks for the info anyway
Eoin
I think you can just test for an element's existence by doing
if (document.getElememtById(countries
))
{
}
mewso
stop making the call multiple times though:-
var item = document.getElementById(countries
);
if (item) {
item.className = "new class";
}