<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Comments on snippet: 'addClass, removeClass, hasClass'</title>
<link>http://snipplr.com</link>
<description>Snipplr comments feed'</description>
<language>en-us</language>
<pubDate>Fri, 24 May 2013 12:27:21 GMT</pubDate>
<item>
<title>Luhring said on 7/19/12</title>
<link>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</link>
<description><![CDATA[ Sorry, the browser returns this:
<code>  

 
Or if for some reason that code doesn't display again:


div id="div1" class="  hide"

/div 

div2 id="div2" class="  cf makeFull"
/div

Side-note: 
Dude, where is the edit post button. ]]></description>
<pubDate>Thu, 19 Jul 2012 06:50:06 GMT</pubDate>
<guid>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</guid>
</item>
<item>
<title>Luhring said on 7/19/12</title>
<link>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</link>
<description><![CDATA[ I don't know if I'm messing something up in my code for sure...but for some reason this:
<code>
    var div1 = document.getElementById('div1');
    var div2 = document.getElementById('div2');
    var failsafe = document.getElementsByClassName('failsafe');
    var hide = document.getElementsByClassName('hide');
    var listener1 = addEvent(div1,'mouseover',toggleDiv);
    var listener2 = addEvent(div1,'mouseover',toggleDiv);
    for (var i=0; i< failsafe.length; i++){
        var singleFailsafe = failsafe[i];
    }
    function toggleDiv(){
        if(hasClass(div1,'failsafe')&amp;&amp;hasClass(div2,'failsafe')){
            //hide div1 by removing the failsafe class
            // and adding the hide class
            removeClass(div1,'failsafe')
            addClass(div1,'hide')
            //make div2 take up all of the containing div
            //within branding by removing failsafe and adding makeFull
            removeClass(div2,'failsafe')
            addClass(div2,'cf')
            addClass(div2,'makeFull')
        }else if(hasClass(div1,'hide')&amp;&amp;hasClass(div2,'makeFull')){
            //resize div2 by switching out cf and makeFull to failsafe.
            removeClass(div2,'makeFull')
            removeClass(div2,'cf')
            addClass(div2,'failsafe')
            //show div1 by switching hide back to failsafe.
            removeClass(div1,'hide')
            addClass(div1,'failsafe')
        }
    }
</code>

in the browser returns this:

<code>


</code>

notice the 2 spaces added before hide when either the hide class is applied or when the failsafe class is removed. the same thing happens to div 2...it adds 2 spaces before cf but only one space between cf and makeFull (like it's supposed to). 
I'm relatively new to js but I don't understand the regular expressions thing and I've tried removing all of the spaces in my code and that didn't work... ]]></description>
<pubDate>Thu, 19 Jul 2012 06:38:52 GMT</pubDate>
<guid>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</guid>
</item>
<item>
<title>Ellsass said on 3/29/11</title>
<link>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</link>
<description><![CDATA[ @Kerrick: You actually need to replace it with a space. If you are removing a class that falls between two other classes, replacing it with an empty string will join those other two classes.

Given: `elem.className = 'one two three';`

Replacing `\stwo\s` with an empty string results in: `elem.className = 'onethree';`

Instead, replace with a single space as shown in the original example, and then replace multiple spaces with a single space:
`elem.className = elem.className.replace(reg,' ').replace(/\s+/g,' ');`

Then (in my example) you will be left with `elem.className = 'one three';`. 

You may also want to add `.replace(/^\s|\s$/,'')` to trim spaces from the very beginning or end of the `.className` property to take care of classes that were removed from the very beginning or end of the string. ]]></description>
<pubDate>Tue, 29 Mar 2011 02:07:47 GMT</pubDate>
<guid>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</guid>
</item>
<item>
<title>Kerrick said on 1/14/11</title>
<link>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</link>
<description><![CDATA[ I recommend adding this line to the end of `addClass()`

    ele.className.replace(/ +/g,' ');

And changing this line of `removeClass()`

    ele.className=ele.className.replace(reg,' ');

to this:

    ele.className=ele.className.replace(reg,'');

Doing so will remove all the extraneous spaces. ]]></description>
<pubDate>Fri, 14 Jan 2011 19:14:07 GMT</pubDate>
<guid>http://snipplr.com/view/3561/addclass-removeclass-hasclass/</guid>
</item>
</channel>
</rss>