Toggle div visibility

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jun 2008
Posts: 13
Reputation: AliHurworth is an unknown quantity at this point 
Solved Threads: 0
AliHurworth AliHurworth is offline Offline
Newbie Poster

Toggle div visibility

 
0
  #1
Oct 25th, 2009
Hi all,

I have a div I would like to show and hide.

I've used a script before which works, but now reusing it I can't make it fly. As I can't see where I am going wrong, can anyone else help?

This is the one that works:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!--script in head tag -->
  2. <script type="text/javascript">
  3. <!--
  4. function toggleBox(szDivID, iState) // 1 visible, 0 hidden
  5. {
  6. if(document.layers) //NN4+
  7. {
  8. document.layers[szDivID].visibility = iState ? "show" : "hide";
  9. }
  10. else if(document.getElementById) //gecko(NN6) + IE 5+
  11. {
  12. var obj = document.getElementById(szDivID);
  13. obj.style.visibility = iState ? "visible" : "hidden";
  14. }
  15. else if(document.all) // IE 4
  16. {
  17. document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
  18. }
  19. }
  20. // -->
  21. </script>
  22.  
  23. <body>
  24. <div id="positioned-element25">
  25. <a href="#" onClick="toggleBox('login',1);">Login</a></div>
  26. <div id="login">
  27. <form name="fp_login" action="prcs_login.php" method="post">
  28. <table>
  29. <!-- abbreviated -->
  30. <tr>
  31. <td>
  32. ...
  33. <td>
  34. <input type="button" onClick="toggleBox('login',0);" value="Cancel">
  35. </td>
  36. </tr>
  37.  
  38.  
  39. </table>
  40. </form>
  41. </div>

...and this is the new version:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. <!--
  3. function toggleBox(szDivID, iState) // 1 visible, 0 hidden
  4. {
  5. if(document.layers) //NN4+
  6. {
  7. document.layers[szDivID].visibility = iState ? "show" : "hide";
  8. }
  9. else if(document.getElementById) //gecko(NN6) + IE 5+
  10. {
  11. var obj = document.getElementById(szDivID);
  12. obj.style.visibility = iState ? "visible" : "hidden";
  13. }
  14. else if(document.all) // IE 4
  15. {
  16. document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
  17. }
  18. }
  19. // -->
  20. </script>
  21. <body>
  22. <div id="positioned-element25">
  23. <a href="#" onClick="toggleBox('login',1);">Login</a></div>
  24. <div id="login">
  25. <?php
  26. include ('mailer.php');
  27. ?>
  28. </div>
  29. ...

I can't see the difference, but the top one works and not the bottom. Pointers please?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #2
Oct 25th, 2009
Ali,

The problem may be that toggleBox doesn't actually have a toggle action. It shows/hides a dom element depending on what is passed to it as iState.

In other words, the reponsibility for which state becomes set, is external to toggleBox . If iState is always 1 or always 0 then the visibility state of 'login' will never change.

I thknk you should ensure that 'login' is initially hidden. This is best achieved with css:
  1. #login { visibility : hidden; }
Then at least the Login link will cause it to become visible even if nothing ever hides it again.

Airshow
Last edited by Airshow; Oct 25th, 2009 at 7:38 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 13
Reputation: AliHurworth is an unknown quantity at this point 
Solved Threads: 0
AliHurworth AliHurworth is offline Offline
Newbie Poster

Thank-you Airshow...

 
0
  #3
Oct 25th, 2009
In the end I did it like so:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. <!--
  3. function toggleBox(szDivID, iState) // 1 visible, 0 hidden
  4. {
  5. if(document.getElementById) //gecko(NN6) + IE 5+
  6. {
  7. var obj = document.getElementById(szDivID);
  8. obj.style.display = iState ? "block" : "none";
  9. }
  10.  
  11. }
  12. // -->
  13. </script>

and

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="positioned-element25">
  2. <a href="#" onClick="toggleBox('login',1);">Login</a></div>
  3. <div id="login" style="display:none">
  4. <?php
  5. include ('mailer.php');
  6. ?>
  7. </div>

...which takes into account your suggestion as well as simplifying the script.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 614 | Replies: 2
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC