Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Created May 15, 2017 07:16
Show Gist options
  • Save dmjcomdem/7fb2a75554568fa256f262c48302d4a3 to your computer and use it in GitHub Desktop.
Save dmjcomdem/7fb2a75554568fa256f262c48302d4a3 to your computer and use it in GitHub Desktop.
Функция для задания/ удаления класса addClass(elem, class) removeClass(elem, class)
/*
* @param {DOM Element Object} o
* @param {String} c name class
*/
function addClass(o, c) {
var re = new RegExp("(^|\\s)" + c + "(\\s|$)", "g");
if (re.test(o.className)) return
o.className = (o.className + " " + c).replace(/\s+/g, " ").replace(/(^ | $)/g, "");
}
function removeClass(o, c){
var re = new RegExp("(^|\\s)" + c + "(\\s|$)", "g");
o.className = o.className.replace(re, "$1").replace(/\s+/g, " ").replace(/(^ | $)/g, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment