Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

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

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #37117
    Al3ks
    Participant

    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:


    onClick="document.getElementById('b1').style.display='none';"

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

    #98879
    karlpcrowley
    Participant

    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

    #98881
    Al3ks
    Participant

    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

    #98882
    karlpcrowley
    Participant

    I was thinking of a for loop to do it but I hit a brick wall
    http://jsfiddle.net/48ypF/1/

    Can anybody see where I went wrong?

    #98888
    karlpcrowley
    Participant



    Hello 1

    Hello 2

    Hello 3



    ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

    Figured it out :)

    #98890
    Al3ks
    Participant

    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.

    #98892
    karlpcrowley
    Participant

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.