Lorem ipsum dolor sit amet, consectetur adipisicing" />

Lorem ipsum dolor sit amet, consectetur adipisicing" />

Lorem ipsum dolor sit amet, consectetur adipisicing"/>

DEVHIDE
  • Home (current)
  • About
  • Contact
  • Cookie
  • Home (current)
  • About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy
  • TOS
Login Or Sign up

Clearing Class using Jquery prior using toggleClass

164 Views Asked by Neil At 31 August 2013 at 00:41 2025-12-20T07:54:13.254808

Initial - UPDATE:

HTML:

<li class="span3 trainer-profiles active">
    <aside class="trainer-info">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur, fugit hic ex delectus fugiat quidem atque esse. Quod, pariatur, voluptatem.</p>
    </aside>

    <img src="/img/placeholder/trainer-pic.png" alt="" class="trainer-pic">
        <h2 class="trainer-name">name</h2>
        <a href="#" class="trainer-icon"></a>
</li>

JS:

$(".trainer-profiles > a.trainer-icon").on("click", function(e){
    e.preventDefault();
    $(this).parent().toggleClass("active");
});
$(".trainers-view-all").on("click", function(e){
    e.preventDefault();
    $(".trainer-profiles").siblings().toggleClass("active");
});

As you can see one section shows the immediate child of the click. The other shows all. However if the class "active" is already present, as the other block of code gets called the script removes the "active" due to the toggleClass api.

What is the quickest way of removing all the prior active classes before using the toggleClass api. As at the moment any class with an active class present gets removed as the others get "active" added.

Any help is welcome :)

Many thanks!

------> Solution

&.show-all{
    @extend .trainer-profiles.active;
}

Created a new class, extending the prior class gives me the same result.

jQuery:

$(".trainers-view-all").on("click", function(e){
    e.preventDefault();
    $(".trainer-profiles").siblings().toggleClass("show-all");
});

By adding the .show-all class on toggle, I mimicked the class behavior and as it sits within a toggleClass. When the class gets removed it is does not negate the initial .active class.

I am pretty sure I could get the jQuery to a smaller state but it is effective at present.

jquery toggleclass
Original Q&A
1

There are 1 best solutions below

3
Arun P Johny Arun P Johny On 31 August 2013 at 00:45

here we are not looking to toggle the state... just add the active class to all elements since the option says viewAll

$(".trainer-profiles > a.trainer-icon").on("click", function(e){
    e.preventDefault();
    $(this).parent().toggleClass("active");
});
$(".trainers-view-all").on("click", function(e){
    e.preventDefault();
    $(".trainer-profiles").addClass("active");
});

Related Questions in JQUERY

  • In Datatables, start value resets to 0, when column sorting
  • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
  • window.location.href redirects but is causing problems on the webpage
  • Using JQuery Date Slider
  • Storing selected language in localStorage
  • How to stop other divs from still showing when i click a different button?
  • Check multiple values with jQuery
  • Bootstrap component does not want to render in Datatables function
  • put white spaces when entering an amount moneytype symfony
  • Trouble accessing custom header in AJAX response using jQuery in Fiware Keyrock
  • I just cant make it work, HTML, JS and Firebase error
  • Didn't declared variable still not getting any error in JavaScript
  • Move element horizontally while scrolling vertically in pure JavaScript
  • allow multi carousel in same page
  • Embedded TikTok posts / thumbnail styling issue

Related Questions in TOGGLECLASS

  • how to switch between classes in JS
  • Toggle classes with jQuery by clicking button and clicking other element than selected
  • Why isn't this toggleClass() working?
  • why does changing of class of a element distorts the view?
  • Toggle class only on the div that is being hovered
  • Trying to only show header when accordion menu is not active
  • Jquery Find Active Class And Replace that class
  • toggleClass won't change class
  • Why does jQuery.fadeTo not animate if toggleClass is called in the callback?
  • Why does my toggleClass event add the class, but not remove it on subsequent clicks?
  • WordPress menu and bootstrap tab intergration
  • Jquery each toggleClass
  • Font-Awesome icon won't change onclick
  • On div click reveal list item with the same class
  • JQuery Toggle icon with a variety of different icons issue

Trending Questions

  • UIImageView Frame Doesn't Reflect Constraints
  • Is it possible to use adb commands to click on a view by finding its ID?
  • How to create a new web character symbol recognizable by html/javascript?
  • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
  • Heap Gives Page Fault
  • Connect ffmpeg to Visual Studio 2008
  • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
  • How to avoid default initialization of objects in std::vector?
  • second argument of the command line arguments in a format other than char** argv or char* argv[]
  • How to improve efficiency of algorithm which generates next lexicographic permutation?
  • Navigating to the another actvity app getting crash in android
  • How to read the particular message format in android and store in sqlite database?
  • Resetting inventory status after order is cancelled
  • Efficiently compute powers of X in SSE/AVX
  • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

Popular # Hahtags

javascript python java c# php android html jquery c++ css ios sql mysql r reactjs node.js arrays c asp.net json

Popular Questions

  • How do I undo the most recent local commits in Git?
  • How can I remove a specific item from an array in JavaScript?
  • How do I delete a Git branch locally and remotely?
  • Find all files containing a specific text (string) on Linux?
  • How do I revert a Git repository to a previous commit?
  • How do I create an HTML button that acts like a link?
  • How do I check out a remote Git branch?
  • How do I force "git pull" to overwrite local files?
  • How do I list all files of a directory?
  • How to check whether a string contains a substring in JavaScript?
  • How do I redirect to another webpage?
  • How can I iterate over rows in a Pandas DataFrame?
  • How do I convert a String to an int in Java?
  • Does Python have a string 'contains' substring method?
  • How do I check if a string contains a specific word?

Copyright © 2021 Jogjafile Inc.

  • Disclaimer
  • Privacy
  • TOS
  • Homegardensmart
  • Pricesm.com
  • Aftereffectstemplates