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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

javascript document.getElementById(array item)

  • 23-07-2008 4: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,263 ✭✭✭✭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,263 ✭✭✭✭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,263 ✭✭✭✭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,035 Mod ✭✭✭✭mewso


    stop making the call multiple times though:-

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


  • Advertisement
Advertisement