Skip to content

Instantly share code, notes, and snippets.

@dannycroft
Created April 13, 2011 20:46
Show Gist options
  • Save dannycroft/918374 to your computer and use it in GitHub Desktop.
Save dannycroft/918374 to your computer and use it in GitHub Desktop.
Was asked to toggle some stuff without a framework
var init = {
accordion: function(){
// Grab next (not null) element
function next(elem) {
do {
elem = elem.nextSibling;
} while (elem && elem.nodeType != 1);
return elem;
}
// Grab definition term
var dt = document.getElementsByTagName('dt');
// Loop through all dt
for (var i=0; i < dt.length; i++) {
// Assign each a click event
dt[i].onclick = function() {
// Assign next element dd and child span
var dd = next(this);
var span = this.lastChild;
// Check/toggle display style
dd.style.display = (dd.style.display != 'none' ? 'none' : '' );
// Switch background position on span
span.style.backgroundPosition = (span.style.backgroundPosition != 'left 0px' ? 'left 0px' : 'left -28px');
};
};
}
};
init.accordion();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment