Skip to content

Instantly share code, notes, and snippets.

@brendanfalkowski
Created December 7, 2011 19:18
Show Gist options
  • Save brendanfalkowski/1444200 to your computer and use it in GitHub Desktop.
Save brendanfalkowski/1444200 to your computer and use it in GitHub Desktop.
onClick toggle the next sibling of an element
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>
*
*/
@brendanfalkowski
Copy link
Author

@realto619
Copy link

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