Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery hasClass returns true but removeClass doesn't work?
    text
    copied!<p>This has been fixed. It was poor programming. I was calling the function twice. so it was removing the class but when the function ran the second time it was readding the class. Sorry for my poor programming and thank you all for trying to help. I love stackoverflow because of all the helpful people!</p> <p>Alright so I have been trying to figure out something but it just might be my cold that is stopping me from solving it. What I am doing is when the user selects something it adds a class. but than if they select it again I want to remove the class. So I checked to see if the element hasClass which returns true but than when I do removeClass it does nothing...</p> <p>Modified the JS to look like this. To show that I can see that the Selectedindex and index do match and that the removeClass for "option-select" is being skipped/ignored for some reason. I can change it to say removeClass("option") and it works just fine but I can not removeClass("option-selected")</p> <pre><code>$(obj.find('.option')).each(function(Selectedindex) { if ($(this).hasClass('option-selected') &amp;&amp; Selectedindex == index) { $(this).removeClass('option-selected'); console.log(Selectedindex+" == "+index); } }); </code></pre> <p>index is passed to the function when the element is selected.</p> <p>here is some of the HTML</p> <pre><code>&lt;div id="MultipleSelect-HTML" class="dropdown container" multiple="multiple" style="width: 100%;"&gt; &lt;ul class="options" style="width: 100%; display: block; position: relative;"&gt; &lt;li&gt; &lt;a class="option option-selected"&gt; &lt;input class="option-value" type="hidden" value="0"&gt; &lt;img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-facebook.png"&gt; &lt;label class="option-text" style="cursor:pointer;"&gt;Facebook&lt;/label&gt; &lt;small class="option-description desc"&gt;Check out my Facebook page!&lt;/small&gt; &lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a class="option option-selected"&gt; &lt;input class="option-value" type="hidden" value="1"&gt; &lt;img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-twitter.png"&gt; &lt;label class="option-text" style="cursor:pointer;"&gt;Twitter&lt;/label&gt; &lt;small class="option-description desc"&gt;Check out my Twitter page!&lt;/small&gt; &lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a class="option"&gt; &lt;input class="option-value" type="hidden" value="2"&gt; &lt;img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-linkedin.png"&gt; &lt;label class="option-text" style="cursor:pointer;"&gt;LinkedIn&lt;/label&gt; &lt;small class="option-description desc"&gt;Check out my LinkedIn page!&lt;/small&gt; &lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a class="option"&gt; &lt;input class="option-value" type="hidden" value="3"&gt; &lt;img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-flickr.png"&gt; &lt;label class="option-text" style="cursor:pointer;"&gt;Flickr&lt;/label&gt; &lt;small class="option-description desc"&gt;I don't have a flicker Page :(&lt;/small&gt; &lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>Please keep in mind that all of this information is dynamically generated using javascript</p> <p>Thanks for any help that you can provide.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload