Show / Hide Div Multiple Div's

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 1sae
    New Member
    • Jun 2010
    • 1

    Show / Hide Div Multiple Div's

    Let me start by saying I'm a noob to JavaScript.

    What I'm trying to do for my website is have a select menu that shows the number of div's that's selected. I found a JS that I could do that with, but it only toggles, not change specifically what's selected. So if you click the wrong one, it doesn't work right. Here's what I got:
    Code:
    <body>
      <script type="text/javascript">
    function toggle_visibility(){
    	for(var i = 0,len = arguments.length;i < len; i++){
        var e = document.getElementById(arguments[i]).style,d = e.display;
        e.display = (d == "block") ? "none" : "block";
      }
    }
    </script>
    
      <select name="select" id="select">
        <option>0</option>
        <option onclick="toggle_visibility('1');" >1</option>
        <option onclick="toggle_visibility('1','2');">2</option>
        <option onclick="toggle_visibility('1','2','3');">3</option>
        <option onclick="toggle_visibility('1','2','3','4');">4</option>
      </select><br>
    <div id="1" style="display: none;">Test1</div>
    <div id="2" style="display: none;">Test2</div>
    <div id="3" style="display: none;">Test3</div>
    <div id="4" style="display: none;">Test4</div>
    </body>
    Any help would be appreciated, cause I'm lost.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Code:
    e.display = (d == "block") ? "none" : "block";
    change the above line to simple
    e.display="inli ne" or whatever

    and before your for loop add another loop that will hide all the div. I think your problem will be solved.
    And If I understand what you want, Then I am sure this is not a smart solution at all.

    Comment

    Working...