Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add and remove class onClick action

 <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

like image 938
Dhanush Bala Avatar asked Jun 16 '14 11:06

Dhanush Bala


People also ask

How do I add and delete class on click?

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.

How do I delete a class in Click JS?

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.


2 Answers

Try this.

$(".fbutton").click(function (e) {
$(this).addClass("fcurrent").siblings().removeClass("fcurrent");
});

DEMO

like image 81
Sridhar R Avatar answered Oct 11 '22 09:10

Sridhar R


$(".fbutton").click(function(e) {
    $(".fbutton").removeClass("fcurrent");
    $(this).addClass("fcurrent");
});
like image 34
Joakim M Avatar answered Oct 11 '22 10:10

Joakim M