<button class="fbutton btn pull-right filterevents " id="one" >School Events</button>
<button class="fbutton btn pull-right filterevents" id="two" >Zone Events</button>
<button class="fbutton btn pull-right filterevents" id="three" >My Events</button>
I need to add class "selected" to specific button onClick.
my code is
$("#one").click(function(e) {
$(this).addClass("fcurrent");
$("#two").removeClass("fcurrent");
$("#three").removeClass("fcurrent");
});
if i use instead of id to class like following ,
$(".fbutton").click(function(e) {
$(this).addClass("fcurrent");
});
then how to remove the fcurrent class to another two buttons
jQuery toggleClass() Method Using the jQuery toggleClass() can add or remove one or more classes from the selected html elements. Incase if the selected html element already has the class, then it is removed. if an element does not have the class, then it is added.
removeClass('selected'); $(this). addClass('selected'); }); this is the target of the click event toggleClass method adds a class if it is not present else removes it.
Try this.
$(".fbutton").click(function (e) {
$(this).addClass("fcurrent").siblings().removeClass("fcurrent");
});
DEMO
$(".fbutton").click(function(e) {
$(".fbutton").removeClass("fcurrent");
$(this).addClass("fcurrent");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With