6

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");

  });
});
1
  • If the .btn is inside or next to the .row-offcanvas, I'd just use CSS. Commented Dec 29, 2014 at 17:33

5 Answers 5

18

.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.

Sign up to request clarification or add additional context in comments.

2 Comments

@T.J.Crowder did that already ;) But sure, smaller = more fun :)
Or even: pastie.org/9803880 But that's a pain to debug. :-) Your edit just now is absolutely how I'd do it.
2

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")

Comments

1
$(document).ready(function () {
  $('[data-toggle="offcanvas"]').click(function () {
    if ($('.row-offcanvas').toggleClass('active').hasClass('active'))
      $('.btn').html("HIDE");
    else 
      $('.btn').html("SHOW");
  });
});

Comments

1

I think you are looking for something like this. You just need to add an if/else statement to check the class

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

  if ($('.row-offcanvas').hasClass('active')){
    $('.btn').html("HIDE");
   }
  else{
   $('.btn').html("SHOW");
   }

 });
});

http://jsfiddle.net/GregWebBandit/Lmfok39k/

Comments

0

You can use javascript by checking the element classList Like this:

var el = document.getElementById("elementId");
el.classList.contains("foo");

More information in Mozilla Docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.