Hide/Show multiple divs without getElementById

Discussion in 'JavaScript' started by cangoalie, Mar 14, 2008.

  1. #1
    Hi
    I need to hide/show multiple divs with the clicking of a link. Basically, I want to have one link that allows the user to view all divs, then four links that select only one of my four "types" of divs (Meetings, Tables, Events, Other). The problem with this that I cannot solve is this: on the page I am building I will have more than one div of each type - for example, maybe three Meetings, two Tables, four Events and one Other. This prohibits me from using a JavaScript method using getElementById, as ids have to be individual. I have googled the hell out of this question but don't have any answers. I also realize that I need to use a non-unique attribute like "class" rather than id but I don't know how to accomplish this. Any help you can offer would be greatly appreciated.
     
    cangoalie, Mar 14, 2008 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can either use a container DIV for each category, using an ID that is unique for each category, like:

    <div id='MeetingsContainer'>
    <div id='foo'>meeting stuff</div>
    <div id='foo1'>meeting stuff</div>
    etc....
    </div>
    <div id='TablesContainer'>
    <div id='somethingelse'>tables stuff</div>
    </div>

    OR, you can assign a unique id to each div, having the category embedded in the id:

    <div id='Meetings_01'>...</div><div id='Tables_01'></div><div id='Meetings_02'></div>... etc etc

    In your javascript you can use an indexOf function to see if a particular div contains your category name.
     
    GreatMetro, Mar 17, 2008 IP
  3. jeremybass

    jeremybass Guest

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey i use this to hide show many divs on the same page... i hope i understud you right

    <script language="JavaScript"  type="text/javascript">
    <!--
    function toggleDiv(divid){
        if(document.getElementById(divid).style.display == 'none'){
          document.getElementById(divid).style.display = 'block';
          document.pageLoading.TCallLabel('/','restart_function');
              }else{
          document.getElementById(divid).style.display = 'none';
        }
      }
    //-->
    
    </script>
    Code (markup):
    Just put in your <a...></a>
    onmousedown="javascript:toggleDiv('hideMe3')"
    Code (markup):
    you can use
    hideMe3
    hideMe4
    hideMe5 ect....
    hope that helps
    and use div id to match
     
    jeremybass, Mar 17, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey, you might've gotten the answer you need already considering this thread is a couple days old, though you haven't replied yet. You should check out jQuery. I've found that it makes coding in JavaScript much easier.

    You could first give all your DIVs the arbitrary class that explains what they are and then with jQuery, you could do something like:
    $('.their_class').hide()
     
    zerxer, Mar 22, 2008 IP