Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery open div with containing li class=“active”

I currently use this code to hide and display a div:

<div id="showmenu">Click Here</div>
<div class="menu" style="display: none;"><ul><li>Button1</li><li>Button2</li><li>Button3</li></ul></div>

jQuery:

$(document).ready(function() {
    $('#showmenu').click(function() {
            $('.menu').slideToggle("fast");
    });
});

When clicking on the div "Click Here" with id="showmenu", I want to add the class "active" to this div.

How can I achieve that?

like image 341
JGeer Avatar asked Mar 13 '26 02:03

JGeer


1 Answers

You can use toggleClass:

$('#showmenu').click(function() {
    $( this ).toggleClass("active");
    $('.menu').slideToggle("fast");
});
like image 189
antyrat Avatar answered Mar 14 '26 15:03

antyrat



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!