Created
April 13, 2011 20:46
-
-
Save dannycroft/918374 to your computer and use it in GitHub Desktop.
Was asked to toggle some stuff without a framework
This file contains 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
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