Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close current if you click on it #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions :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 <a> tag
linkElements[0].focus();
};
35 changes: 15 additions & 20 deletions 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");
Expand All @@ -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 <a> tag
linkElements[0].focus();
linkElements[0].focus();
};