Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting class toggle in jQuery

I am trying to do an off-canvas sidebar and I am using the .toggleClass function to make it active or not. When it is active I would like the button (.btn) to say "hide" and when it is not say "show". I have already tried to do an if statement and it has failed. I have also looked at other stackoverflow questions with no success. Can anybody help with how to detect a class has been toggled or not?

$(document).ready(function () {
  $('[data-toggle="offcanvas"]').click(function () {
    $('.row-offcanvas').toggleClass('active');

    // if active "hide"
    $('.btn').html("HIDE");

    // if not active "show"
    $('.btn').html("SHOW");

  });
});
like image 912
iampj Avatar asked Dec 29 '14 17:12

iampj


People also ask

How do I check my toggle class?

jQuery toggleClass() Method The toggleClass() method toggles between adding and removing one or more class names from the selected elements. This method checks each element for the specified class names. The class names are added if missing, and removed if already set - This creates a toggle effect.

How do I get rid of toggle class?

The jQuery toggleCLass() method is used to add or remove one or more classes from the selected elements. This method toggles between adding and removing one or more class name. It checks each element for the specified class names. If the class name is already set, it removes and if the class name is missing, it adds.

How do I toggle Show hide in jQuery?

jQuery toggle() MethodThe toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

What does toggleClass return?

toggleClass( className )Returns: jQuery. Description: Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.


2 Answers

.hasClass('someClass') will help you to retrieve a boolean true/false

api.jquery.com/hasclass: Determine whether any of the matched elements are assigned the given class. The .hasClass() method will return true if the class is assigned to an element

$(document).ready(function () {
  $('[data-toggle="offcanvas"]').click(function () {
    $('.row-offcanvas').toggleClass('active');
    var act = $('.row-offcanvas').hasClass("active");

    if(act){
       $('.btn').html("HIDE");
    }else{
       $('.btn').html("SHOW");
    }
  });
});

a shorter way using a Conditional Operator (AKA Ternary operator) would be:

$(function() {  // DOM ready

   $('[data-toggle="offcanvas"]').on("click", function () {
     var $row = $('.row-offcanvas').toggleClass('active');
     $('.btn').html($row.hasClass("active") ? "HIDE" : "SHOW");
   });

});

Also I have to warn you that by selecting elements like $(".btn") will alter every single .btn element on the page. Make always sure to use the right selectors with the help of .find() (or similar Selector methods). for specificity sake.

like image 62
Roko C. Buljan Avatar answered Sep 24 '22 00:09

Roko C. Buljan


You should have another way to select your objects, for example by id, as follows:

$("#myid")...

Then you can use the hasClass function (http://api.jquery.com/hasclass/) to verify if the class has already been added to the object.

$("#myid").hasClass("xxx")
like image 30
Nicola Ferraro Avatar answered Sep 21 '22 00:09

Nicola Ferraro