Treehouse: Grow your CSS skills. Land your dream job.

document.getElementById – How to make it get more than one id

  • # March 12, 2012 at 6:54 pm

    Hey guys, I’m not sure if that’s possible but I ‘m trying to somehow make this code to get more than on element id instead of just one Id. Here’s what I have:


    Anyone knows how to do this?
    Maybe any alternative ways?

    # March 12, 2012 at 7:37 pm

    There are a lot of ways to do this, but the easiest of all would be to use document.getElementsByClassName instead of ID
    That’s if it’s possible to add classes in your scenario

    # March 12, 2012 at 8:01 pm

    I tried different ones like .getElementsByClassName .getElementsByName but it only works for “id” (for one element).

    The thing is I have the individual id’s for separate images in a table.
    And when clicked on a link(s) using the code above I want to make multiple elements disappear/appear.

    I used some complicated javascript code before but it only allowed me to make one link but hide multiple elements.

    Any other suggestions? But thanks for the help anyway

    # March 12, 2012 at 8:21 pm

    I was thinking of a for loop to do it but I hit a brick wall

    Can anybody see where I went wrong?

    # March 12, 2012 at 8:46 pm

    Hello 1

    Hello 2

    Hello 3


    Figured it out :)

    # March 12, 2012 at 9:01 pm

    Thank you so much! This works great, but there is only one little problem, how do I apply this to hide id1 and id3 using one button and then hide id2 using another button?

    I’m not that good with javascript.

    I hope I’m not taking too much of your time.

    # March 12, 2012 at 9:07 pm

    Hello 1

    Hello 2

    Hello 3

    slight rework, not sure if its the best way, but it works :D

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.