show/hide div one at a time JavaScript and jquery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddsal
    New Member
    • Dec 2011
    • 8

    show/hide div one at a time JavaScript and jquery

    so i have this code...that includes three buttons in each div, and i only want to show one div at a time. so when i click a div1, the other divs will not open and when div1 is currently is opened, when i click the div2, div1 will close and div2 is now the current div opened and so on..

    is it possible that i will not create multiple functions for each div, and change my div class and button class will be retained as much as possible....??

    here is my code i just got this js from the net too...

    Code:
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    $(document).ready(function(){
    
        $(".slidingDiv").hide();
    	$(".show_hide").show();
    	
    	$('.show_hide').click(function(){
    	$(".slidingDiv").slideToggle();		
    	});	
    });
    
    </script>
          <button class="show_hide"> Add New</button>
          <br />
          <div class="slidingDiv">
            <form action="" name="add" method="post">
                </strong>
                <tr>
                  <td>form1:</td>
                  <td width="225"><input type="text" name="amount" value="" />
                  </td>
                </tr>             
                 <tr>
                  <td><input type="submit" name="Submit" value="Submit" />
                    <input type="reset" value="Reset">
                  </td>
                </tr>
            </form>
          </div>
    	  
    	      <button class="show_hide">Add New</button>
          <br />
          <div class="slidingDiv">
            <form action="" name="add" method="post">
                </strong>
                <tr>
                  <td>form2</td>
                  <td width="225"><input type="text" name="amount" value="" />
                  </td>
                </tr>             
                 <tr>
                  <td><input type="submit" name="Submit" value="Submit" />
                    <input type="reset" value="Reset">
                  </td>
                </tr>
            </form>
          </div>
    	  
    	  	      <button class="show_hide">Add New</button>
          <br />
          <div class="slidingDiv">
            <form action="" name="add" method="post">
                </strong>
                <tr>
                  <td>form3</td>
                  <td width="225"><input type="text" name="amount" value="" />
                  </td>
                </tr>             
                 <tr>
                  <td><input type="submit" name="Submit" value="Submit" />
                    <input type="reset" value="Reset">
                  </td>
                </tr>
            </form>
          </div>
    i would really appreciate your help guys....
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    Your markup is broken. Fix that before using jquery on it.

    Comment

    • jeddsal
      New Member
      • Dec 2011
      • 8

      #3
      thanks so much.....it works great

      Code:
        $(document).ready(function(){
      
             $(".slidingDiv").hide();
             $(".show_hide").show();
      
             $('.show_hide').click(function(){
                 $('.slidingDiv').hide(1);
                 $(this).nextAll().eq(1).show();
             });
         })
      i just modified it a little..so that the currently opened div will still hide when you click the button..thanks

      Comment

      Working...