Created
December 7, 2011 19:18
-
-
Save brendanfalkowski/1444200 to your computer and use it in GitHub Desktop.
onClick toggle the next sibling of an element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gravdept_next(elem) { | |
do { | |
elem = elem.nextSibling; | |
} while (elem && elem.nodeType != 1); | |
return elem; | |
} | |
function gravdept_toggle(elem) { | |
var nextElem = gravdept_next(elem); | |
if (nextElem.style.display==='block') { nextElem.style.display='none'; } | |
else { nextElem.style.display='block'; } | |
} | |
/** | |
* Example markup: | |
* | |
* <a href="#" onclick="gravdept_toggle(this); return false;">Toggle It</a> | |
* <div>Something to show/hide</div> | |
* | |
*/ |
If you want it to work the first time you click using the example above, you need to reverse the if statement in the second function
function gravdept_toggle(elem) {
var nextElem = gravdept_next(elem);
if (nextElem.style.display==='none') { nextElem.style.display='block'; }
else { nextElem.style.display='none'; }
}
Working Example: https://codepen.io/Realto619/pen/wvrJZzb
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With the help of StackOverflow of course:
http://stackoverflow.com/questions/868407/hide-an-elements-next-sibling-with-javascript