Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hasClass doesn't fire up

I have a bootstrap navbar,whenever and I have a button ,when I click the button the navbar items get displayed/hidden. I have a toggleClass("foo") in the click of the button and the class also gets added,but when I try to do hasClass("foo") it does not work.

$(".navbar-toggler").click(function () {
    $('.selectedMenu').remove();
    $(this).toggleClass('active closed');
    if ($(this).hasClass(".active")) {
        alert("as");
    }
});
like image 419
Rehan Avatar asked Jun 17 '26 01:06

Rehan


1 Answers

You don't need the class selector (.) when using hasClass

Change

$(this).hasClass(".active")

to

$(this).hasClass("active")

hasClass

like image 51
Sushanth -- Avatar answered Jun 19 '26 14:06

Sushanth --



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!