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

  • How to sort these using Javascript or Jquery Most effectively
  • Ajax jQuery firing multiple time display event for the same result
  • .hover() seems to overwrite .click()
  • Check for numeric value with optional commas javascript
  • Extending Highmaps Side Effect
  • Array appending after each onclick and loop in javascript
  • how can i append part of a table based on how many tr it has?
  • Play multiple audio files in a slider
  • Remove added set of rows
  • Access property of an object of type [Model] in JQuery
  • AJAX PHP - Reload div after submit
  • proengsoft/laravel-jsvalidation ReferenceError: jQuery is not defined
  • when a checkbox is checked how to display a different hidden element using javascript
  • Get jquery error Uncaught RangeError: Maximum call stack size exceeded
  • Removing only the closest thead on table filtering

Related Questions in TOGGLECLASS

  • Very simple toggleClass not working
  • Change Font Awesome Icon on Click, but return to original icon when next item is clicked
  • Improve JQuery - Switches
  • ToggleClass not adding Jquery Delay?
  • Font-Awesome icon won't change onclick
  • WordPress menu and bootstrap tab intergration
  • Why does my toggleClass event add the class, but not remove it on subsequent clicks?
  • Why does jQuery.fadeTo not animate if toggleClass is called in the callback?
  • toggleClass won't change class
  • Jquery Find Active Class And Replace that class
  • JQuery toggle unbind/bind
  • Using toggleClass on click - jQuery
  • Flipping Flash cards in jQuery
  • css toggleClass in jquery?
  • jQuery - Remove class from any <li> that wasn't just clicked

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

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
  • Math
  • Aftereffectstemplates