Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle class on hover?

I have written the following code:

$(document).ready(function () {
    $("#rade_img_map_1335199662212").hover(function () {
        $("li#rs1").addClass("active");  //Add the active class to the area is hovered
    }, function () {
        $("li#rs1").addClass("not-active");
    });
});

The problem is it doesnt seem to toggle the class on hover?

But how can i get it so that the class toggles based on hover and non-hover..?

like image 433
SOLDIER-OF-FORTUNE Avatar asked Jul 21 '26 18:07

SOLDIER-OF-FORTUNE


1 Answers

Do not add a different class on hover-out just remove the active class

$(document).ready(function(){

  $("#rade_img_map_1335199662212").hover(function(){

      $("li#rs1").addClass("active");  //Add the active class to the area is hovered
  }, function () {
      $("li#rs1").removeClass("active");
  });

});

or if all elements are inactive at first you could use a single function and the toggleClass() method

$(document).ready(function(){

  $("#rade_img_map_1335199662212").hover(function(){
      $("li#rs1").toggleClass("active");  //Toggle the active class to the area is hovered
  });

});
like image 89
Gabriele Petrioli Avatar answered Jul 23 '26 07:07

Gabriele Petrioli