JavaScript (jQuery?): Toggle Multiple Classes With One Click
When the user clicks div.togglethis, I need two things to happen: Add an 'active' class to div.togglethis Add a 'show' class to an outside element div.showthis When the user cli
Solution 1:
You cannot remove togglethis
entirely otherwise the click won't work next time. Also, I'm not removing the class on second div for the same reason, rather I'm calling show/hide as I think that is what you intend to do.
<div class="toggle togglethis">Click me to add "active"</div>
$('div.toggle').click(function(){
if($(this).hasClass('togglethis')) {
$(this).removeClass('togglethis').addClass('active');
$('div.showthis').hide();
}
else {
$(this).removeClass(active).addClass('togglethis');
$('div.showthis').show();
}
});
Solution 2:
You should look at jQuery's addClass
, removeClass
, toggleClass
, and hasClass
methods (API references here, here, here, and here).
You would call these methods in the click handler for div.togglethis
. Give this a try and post another question if you get stuck.
Post a Comment for "JavaScript (jQuery?): Toggle Multiple Classes With One Click"