Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

javascript document.getElementById(array item)

  • 23-07-2008 05:52PM
    #1
    Registered Users, Registered Users 2 Posts: 872 ✭✭✭


    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


Comments

  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭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.


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    So, did you get sorted?


  • Registered Users, Registered Users 2 Posts: 872 ✭✭✭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


  • Registered Users, Registered Users 2 Posts: 21,278 ✭✭✭✭Eoin


    I think you can just test for an element's existence by doing

    if (document.getElememtById(countries))
    {

    }


  • Moderators, Science, Health & Environment Moderators Posts: 9,213 Mod ✭✭✭✭mewso


    stop making the call multiple times though:-

    var item = document.getElementById(countries);
    if (item) {
    item.className = "new class";
    }


  • Advertisement
Advertisement