:: Show / Hide / Toggle - Multiple Divs and Buttons

:: Show / Hide / Toggle - Multiple Divs and Buttons

Hello guys, I'm starting with JavaScript / jQuery.

  1. <html>
  2.  <head>
  3.   <title> New Document </title>



  4. <style>
  5. #button01 {
  6. width:100px;
  7. height:50px;
  8. margin:10px;
  9. padding:6px 0 0 0;
  10. background-color:#f0f0f0;
  11. }

  12. #button01:hover {
  13. background-color:#ffcccc;
  14. }

  15. #button01 a {
  16. display:block;
  17. width:40px;
  18. height:40px;
  19. margin:auto;
  20. background:url("button01.png")
  21. }

  22. #button01 a:hover {
  23. width:40px;
  24. height:40px;
  25. background:url("button01-hover.png")
  26. }

  27. #hidden01 {
  28. display:none;
  29. width:300px;
  30. height:200px;
  31. margin:0 0 10px 0;
  32. border:4px solid #ffcccc;
  33. }

  34. #button02 {
  35. width:100px;
  36. height:50px;
  37. margin:10px;
  38. padding:6px 0 0 0;
  39. background-color:#f0f0f0;
  40. }

  41. #button02:hover {
  42. background-color:#cccccc;
  43. }

  44. #button02 a {
  45. display:block;
  46. width:40px;
  47. height:40px;
  48. margin:auto;
  49. background:url("button02.png")
  50. }

  51. #button02 a:hover {
  52. width:40px;
  53. height:40px;
  54. background:url("button02-hover.png")
  55. }

  56. #hidden02 {
  57. display:none;
  58. width:300px;
  59. height:200px;
  60. margin:0 0 10px 0;
  61. border:4px solid #cccccc;
  62. }
  63. </style>




  64.  </head>

  65.  <body>

  66. <script type="text/javascript">
  67. function toggle(offset){
  68.     var i, x;
  69.     var stuff = Array('hidden01', 'hidden02');  //put all the id's of the divs here in order 
  70.     for (i = 0; i < stuff.length; i++){  //hide all the divs
  71.           x = document.getElementById(stuff[i]);
  72.           x.style.display = "none";
  73.     }
  74.     // now make the target div visible
  75.     x = document.getElementById(stuff[offset]);
  76.     x.style.display = "block";
  77. window.onload = function(){toggle(0);}
  78. }
  79. </script>

  80. <div style="width:300px;">
  81. <div id="button01"><a href="#" onclick="toggle(0);return false"></a></div>


  82. <div id="button02"><a href="#" onclick="toggle(1);return false"></a></div>
  83. </div>

  84. <div id="hidden01">&nbsp;</div>
  85. <div id="hidden02">&nbsp;</div>


  86.  </body>
  87. </html>

That's working, but I want to fix 2 things:

1- Close/Hide hidden divs if I click on it's corresponding button;

2- After clicking a button, fix hover button image. If click again unfix;


Buttons examples:

button01.png



button01-hover.png



button02.png


button02-hover.png