#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Question Jquery multi DIVs hide/show


    i want to show hide multiple divs with jquery or javascript. I seached the web but i find codes that must have unique divs like:

    PHP Code:
    <div id="div1">text</div>
    <
    div id="div2">text</div
    and so on, but i want to add the divs in a foreache php function so i can't have multiple IDs, the foreache is a div wrapper. So, my question is how can i do that ?
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    Well, for the JavaScript (client side code); you could use something like:

    [code=JavaScript]<script>

    function hideAll()
    {
    var all_divs = document.getElementsByClassName("some_class_name");
    for (i=0;i<=all_divs.length-1;i++) {
    all_divs[i].style.display="none";
    }
    }

    function showAll()
    {
    var all_divs = document.getElementsByClassName("some_class_name");
    for (i=0;i<=all_divs.length-1;i++) {
    all_divs[i].style.display="block";
    }
    }

    </script>[/code]

    [code=HTML]<div id="div1" class="some_class_name">text</div>
    <div id="div2" class="some_class_name">text</div>
    <input type="button" onclick="hideAll()" value="Hide All"/>
    <input type="button" onclick="showAll()" value="Show All"/>[/code]

IMN logo majestic logo threadwatch logo seochat tools logo