• All geek questions in one place

    Add and remove multiple classes using jQuery

    <div id="siteFeelingBannar" class="shadowVeryMild"> <ul> <li><div class="autologinImage"></div><span class="statusHover">Affectionate</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Annoyed</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Anxious</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Chatty</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Content</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Curious</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Ecstatic</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Excited</span></li> <li><div class="autologinImage hidden"><span class="statusHover autologinMargin">Free</span></li> </ul> </div> 

    I have the above code snippet that gives a drop down list so people can choose their current feeling.

    You will notice that the first li slightly different from the rest:

      <li><div class="autologinImage"></div><span class="statusHover">Affectionate</span></li> 

    This is the currently selected li , because it does not have a div.hidden class (adds the img - tick mark to show the selected), and it does not have the span.autologinMargin class (gives the other li elements have the required interval so that they all are in the queue).

    I am trying to write jQuery so that it can switch. for example: if someone clicks on another li , then div.hidden and span.autologinMargin , and in the previous selected these 2 classes are added.

    Can someone help me give a push in the right direction with this code.

    +4
    jquery html html-lists
    Adam Dec 29 '11 at 3:44
    source share
    7 answers

    That should work.

     $('#siteFeelingBannar li').click(function(){ // Add the class to div and spans $(this).siblings().find('div').addClass('hidden'); $(this).siblings().find('span').addClass('autologinMargin'); // Remove the classes from div and span. $(this).find('div').removeClass('hidden'); $(this).find('span').removeClass('autologinMargin'); }); 
    0
    Virendra Dec 29 '11 at 3:57
    source share

    Simplify it by adding a class to li and then style with child selectors.

    Thus, all you have to do is add / remove a class from li , not stream elements.

     var lis = $('#siteFeelingBannar li').click(function() { lis.not('.hidden').addClass('hidden'); $(this).removeClass('hidden') }); 

     <div id="siteFeelingBannar" class="shadowVeryMild"> <ul> <li><div class="autologinImage"></div><span class="statusHover">Affectionate</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Annoyed</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Anxious</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Chatty</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Content</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Curious</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Ecstatic</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Excited</span></li> <li class="hidden"><div class="autologinImage"><span class="statusHover">Free</span></li> </ul> </div> 

     #siteFeelingBannar > ul > li { ... } #siteFeelingBannar > ul > li > div.autologinImage { ... } #siteFeelingBannar > ul > li > div.autologinImage > span.statusHover { ... } #siteFeelingBannar > ul > li.hidden { ... } #siteFeelingBannar > ul > li.hidden > div.autologinImage { ... } #siteFeelingBannar > ul > li.hidden > div.autologinImage > span.statusHover { ... } 
    +3
    user1106925 Dec 29 '11 at 3:53
    source share

    You can use code that adds a class to all .autologinImage elements and then removes them from the click element:

     $("#siteFeelingBannar .autologinImage").click(function() { $(this).closest("ul").find(".autologinImage").addClass("hidden"); $(this).removeClass("hidden"); }); 

    You can see how it works here: http://jsfiddle.net/jfriend00/ECrHP/

    You will also have to fix the <div> mismatch in your first <li> in your HTML. <span> should be inside the <div> , and not after it, like everyone else.

    There is no need to dynamically add / remove the autologinMargin class, since you can only control this with CSS based on whether the parent is hidden or not. In fact, you probably don't even need this class. Just set the default rule and override it if .hidden is above it.

    If you want to move the classes that are now in the div to li and adjust the CSS accordingly, then the code could be even simpler:

     $("#siteFeelingBannar li").click(function() { $(this).removeClass("hidden").siblings().addClass("hidden"); }); 
    +1
    jfriend00 Dec 29 '11 at 3:51
    source share

    Earlier answers do not notice that the classes are on a nested <div> , not a <li> .

     $("#siteFeelingBannar li").click(function() { $(this).find("div").removeClass("hidden").end().find("span").removeClass("autologinMargin"); $(this).siblings().find("div")addClass("hidden").end().find("span").addClass("autologinMargin"); }); 
    +1
    Interrobang Dec 29 '11 at 3:53
    source share

    You can just try the switch class property built into jquery.

     $('.classname').hover( function(){ $(this).stop(false, true).switchClass("classname"); }, function(){ $(this).stop(false, true).switchClass("2ndclassname); }); 

    This is an example.

    0
    Gabriel Rivera Dec 29 '11 at 3:51
    source share

    You can add a class to the selected li , say selected . You can then use this to find the selected li and add the appropriate classes to your children.

    Something like that:

     $(".selected").find("autologinImage").addClass("hidden").end() .find("statusHover").addClass("autologinMargin").end() .removeClass("selected"); 

    I would rather define your css instead so you don't have to remove and add these classes. Something like that:

     .autologinMargin{ visibility: hidden; } .selected .autologinMargin{ visibility: visible; } 

    In addition, using visibility will allow you to keep the margin stable, since the image will still occupy a space.

    0
    James montagne Dec 29 '11 at 3:53
    source share

    Here is a working example .

     $('#siteFeelingBannar li').on('click', function() { if (!$(this).children('.autologinImage').hasClass('hidden')) { // Do nothing } else { $('.autologinImage').not('hidden').addClass('hidden'); $(this).find('div.autologinImage').removeClass('hidden'); } }); 
    0
    Josh smith Dec 29 '11 at 4:01
    source share

    Source: https://habr.com/ru/post/1388406/

    More articles:

    • Creation and extension - c
    • Error Boost :: Spirit - c ++
    • How can I get the mkannotation position while dragging it - ios
    • Android insertWithOnConflict returns the wrong value - android
    • How to handle errors when using target package in msbuild? - msbuild
    • emacs python-mode: how to highlight str or unicode str string in different colors - emacs
    • Android video call - android
    • Send data to the front end when updating - java
    • Introduction to Genetic Algorithms - r
    • Why can't CCL download hunchentoot? - ccl

    All Articles

    Geek Questions | 2019