Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

javascript document.getElementById(array item)

Options
  • 23-07-2008 5:52pm
    #1
    Registered Users 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

  • Moderators, Category Moderators, Motoring & Transport Moderators Posts: 21,238 CMod ✭✭✭✭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.


  • Moderators, Category Moderators, Motoring & Transport Moderators Posts: 21,238 CMod ✭✭✭✭Eoin


    So, did you get sorted?


  • Registered Users 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


  • Moderators, Category Moderators, Motoring & Transport Moderators Posts: 21,238 CMod ✭✭✭✭Eoin


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

    if (document.getElememtById(countries))
    {

    }


  • Moderators, Science, Health & Environment Moderators Posts: 8,838 Mod ✭✭✭✭mewso


    stop making the call multiple times though:-

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


  • Advertisement
Advertisement