// window.Helper is loaded from an external script
log2Screen = Helper.log2Screen;
HtmlWrapper = Helper.HtmlWrapper;
// create a span element instance
var span = new HtmlWrapper('span');
// show it on the screen
log2Screen(span.createElement('wait a sec, we will invert this, using 2 classes', 'classaddex'));
// add 2 classnames to span#classaddex after about 1.5 seconds
var el = document.querySelector('#classaddex');
setTimeout(function() { addClass(el, 'backblack'); addClass(el, 'white');}, 1500);
// show another span
log2Screen(span.createElement('click to toggle redOnYellow class', 'classaddex2'));
// use a click handler to toggle, using add-/removeClass
var el2 = document.querySelector('#classaddex2');
el2.style.cursor = 'pointer';
$(el2).on('click', toggleROnY);
function addClass(element, classname){
var currentClassList = (element.className || '').split(/\s+/);
currentClassList.push(currentClassList.indexOf(classname) > -1 ? '' : classname);
element.className = currentClassList.join(' ').trim();
}
function removeClass(element, classname){
var currentClassList = (element.className || '').split(/\s+/)
,classIndex = currentClassList.indexOf(classname) > -1;
if (classIndex > -1) {
currentClassList = currentClassList.splice(classIndex,1);
}
element.className = currentClassList.join(' ').trim();
}
function toggleROnY(){
var currentClassList = (this.className || '').split(/\s+/)
,isroy = currentClassList.indexOf('redOnYellow') > -1
,add = addClass.partial(this)
,remove = removeClass.partial(this)
remove(isroy ? 'redOnYellow' : 'default');
add(isroy ? 'default' : 'redOnYellow');
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.