From 9c7cdbef9c0e30ac5cb702c52757d0540221b07d Mon Sep 17 00:00:00 2001 From: Rob McGinley Date: Wed, 17 Apr 2019 08:43:22 -0400 Subject: [PATCH] Close current if you click on it --- :q | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ faqs.js | 35 +++++++++++++++-------------------- 2 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 :q diff --git a/:q b/:q new file mode 100644 index 0000000..0f66a78 --- /dev/null +++ b/:q @@ -0,0 +1,56 @@ +"use strict"; +var $ = function(id) { return document.getElementById(id); }; + +var current = null; +// the event handler for the click event of each a element +var toggle = function() { + var link = this; // they clicked a tag + var h2 = link.parentNode; // h2 tag that contains a tag + var div = h2.nextElementSibling; // h2 tag's sibling div tag + + var h2Elements = faqs.getElementsByTagName("h2"); + for (var i = 0; i < h2Elements.length; i++ ) { + if( h2Elements[i].hasAttribute("class")) + + { + //h2Elements[i].className=""; + h2Elements[i].removeAttribute("class"); + h2Elements[i].nextElementSibling.removeAttribute("class"); + + } + + + } + + // toggle plus and minus image in h2 elements by adding or removing a class + if (h2.hasAttribute("class")) { +// h2.removeAttribute("class"); + h2.className = ""; + } else { +// h2.setAttribute("class", "minus"); + h2.className = "minus"; + } + + // toggle div visibility by adding or removing a class + if (div.hasAttribute("class")) { + div.className = ""; + } else if (current !== div) { + current = div; + div.className = "open"; + } else { + current = null; + } +}; + +window.onload = function() { + // get the a tags + var faqs = $("faqs"); + var linkElements = faqs.getElementsByTagName("a"); + + // attach event handler for each a tag + for (var i = 0; i < linkElements.length; i++ ) { + linkElements[i].onclick = toggle; + } + // set focus on first tag + linkElements[0].focus(); +}; diff --git a/faqs.js b/faqs.js index 7639333..f6ea292 100755 --- a/faqs.js +++ b/faqs.js @@ -1,10 +1,11 @@ "use strict"; var $ = function(id) { return document.getElementById(id); }; +var current = null; // the event handler for the click event of each a element var toggle = function() { var link = this; // they clicked a tag - var h2 = link.parentNode; // h2 tag that contains a tag + var h2 = link.parentNode; // h2 tag that contains a tag var div = h2.nextElementSibling; // h2 tag's sibling div tag var h2Elements = faqs.getElementsByTagName("h2"); @@ -18,44 +19,38 @@ var toggle = function() { } - // if( h2Elements[i].nextElementSibling.hasAttribute("class")) - // { - // // alert("h2 element sibling of "+i+" has class!"); - // //h2Elements[i].className=""; - // h2Elements[i].nextElementSibling.removeAttribute("class"); - // }else{ - // } } // toggle plus and minus image in h2 elements by adding or removing a class - if (h2.hasAttribute("class")) { -// h2.removeAttribute("class"); + if (h2.hasAttribute("class")) { +// h2.removeAttribute("class"); h2.className = ""; - } else { -// h2.setAttribute("class", "minus"); + } else { +// h2.setAttribute("class", "minus"); h2.className = "minus"; } // toggle div visibility by adding or removing a class - if (div.hasAttribute("class")) { -// div.removeAttribute("class"); + if (div.hasAttribute("class")) { div.className = ""; - } else { -// div.setAttribute("class", "open"); + } else if (current !== div) { + current = div; div.className = "open"; - } + } else { + current = null; + } }; window.onload = function() { // get the a tags var faqs = $("faqs"); var linkElements = faqs.getElementsByTagName("a"); - - // attach event handler for each a tag + + // attach event handler for each a tag for (var i = 0; i < linkElements.length; i++ ) { linkElements[i].onclick = toggle; } // set focus on first tag - linkElements[0].focus(); + linkElements[0].focus(); };