vonko1988

JSTreeView

May 27th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var anchorTags = document.getElementsByTagName('a');
  2. var lenA = anchorTags.length;
  3.  
  4. //first we hide all nested lists and attach onlcick event to all 'a' tags
  5. (function () {
  6.     var nestedLists = document.querySelectorAll('ul ul');
  7.     var len = nestedLists.length;
  8.  
  9.     for (var i = 0; i < len; i++) {
  10.         nestedLists[i].style.display = "none";
  11.     };
  12.  
  13.     for (var i = 0; i < lenA; i++) {
  14.         anchorTags[i].addEventListener('click', expandOrCollapse);
  15.     };
  16. }())
  17.  
  18. //no we define the function that hides/shows the nested lists
  19. function expandOrCollapse() {
  20.     if (this.nextElementSibling.style.display == 'none') {
  21.         this.nextElementSibling.style.display = 'block';
  22.     }
  23.     else if (this.nextElementSibling.style.display == 'block') {
  24.         this.nextElementSibling.style.display = 'none';
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment