Posted By

plindsay on 11/19/08


Tagged

toggle visibility elements


Versions (?)

toggleElements()


 / Published in: JavaScript
 

Utility function using the DOM to toggle the visibility of multiple elements on a page.

  1. function toggleElements() {
  2.  
  3. // toggles the visibility of elements
  4. // sample usage : onclick="toggleElements('show', 'element_1', 'element_2'); etc...
  5. // the 1st argument is the visibility mode (show or hide)
  6. // other arguments are the IDs of the elements to manipulate
  7.  
  8. if (!arguments) return false;
  9. if (arguments[0] != 'show' && arguments[0] != 'hide') return false;
  10.  
  11. var v_mode = arguments[0];
  12.  
  13. for (i = 0; i < arguments.length; i++) {
  14.  
  15. var element_id = arguments[i];
  16.  
  17. if (!document.getElementById(element_id)) continue; // no element with this ID exists
  18.  
  19. if (v_mode == 'show') {
  20. document.getElementById(element_id).style.display = '';
  21. } else {
  22. document.getElementById(element_id).style.display = 'none';
  23. }
  24. }
  25. }

Report this snippet  

You need to login to post a comment.