Created
May 15, 2011 13:50
-
-
Save cfleschhut/973170 to your computer and use it in GitHub Desktop.
Plain JavaScript radioClass Fn
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
/* adapted from Ext Core radioClass */ | |
function hasClass(el, cls) { | |
return new RegExp("(^|\\s)" + cls + "(\\s|$)").test(el.className); | |
} | |
function addClass(el, cls) { | |
if (!hasClass(el, cls)) { | |
el.className += el.className ? " " + cls : cls; | |
} | |
} | |
function removeClass(el, cls) { | |
var rep = el.className.match(" " + cls) ? " " + cls : cls; | |
el.className = el.className.replace(rep, ""); | |
} | |
function radioClass(el, cls) { | |
var siblings = el.parentNode.childNodes; | |
for(var i=0; i<siblings.length; i++) { | |
if(siblings[i].nodeType == 1) { | |
removeClass(siblings[i], cls); | |
} | |
} | |
addClass(el, cls); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment