JavaScript Hover and Click Jquery hover click event jquery,mouse hover click jquery,hover after

I have a link with an hover effect. When you hover it, the text become red and underlined. When you click it, the text stay red and underlined.

My problem is that once you clicked on the link, the hover effect still play... But I do not want this effect.

(I don't use toggle, because it's not supported by the last Jquery : 1.9)

HERE'S MY JS FIDDLE

$("h1").addClass("clicked")

$('h1').click(function() {
    if($(this).hasClass("clicked")) {

        $("span").animate({"width": "145px"}, 300);
    $("h1").css({"color": "red"}, 300);


        $(this).removeClass("clicked");
    } else {

        $("span").animate({"width": "0"}, 300);
    $("h1").css({"color": "black"}, 300);

        $(this).addClass("clicked");

    }
}); 


$('h1').hover(function() {

    if($(this).hasClass("clicked")) {

        $("span").stop().animate({"width": "145px"}, 300);
    $("h1").css({"color": "red"}, 300);
        $(this).removeClass("clicked")
    } else {

        $("span").stop().animate({"width": "0"}, 300);
    $("h1").css({"color": "black"}, 300);

        $(this).addClass("clicked");

    }
}); 
Answer:1

If I didn't misunderstand your question you want to:

  • Hover to make it red and underline
  • Click to make it always red and underlined
  • Click again to enable hover red again

Following part of your code needed changes. hover can take two functions one for onmouseover other for onmouseleave and it becomes like:

$('h1').hover(function() {
  if ($(this).hasClass("clicked")) {
    return;
  }

  $("span").stop().animate({
    "width": "145px"
  }, 300);
  $("h1").css({
    "color": "red"
  }, 300);
}, function() {
  if ($(this).hasClass("clicked")) {
    return;
  }
  $("span").stop().animate({
    "width": "0"
  }, 300);
  $("h1").css({
    "color": "black"
  }, 300);


});

I modified your jsfiddle assuming above points. If you move mouse over, it gets red; take away mouse, goes black, and click to make it always red.

Answer:2

You can unbind events with the unbind jQuery method.

$('h1').click(function() {
    $(this).unbind('mouseenter mouseleave');
    ...
}
Answer:3

I update your fiddle click here

use this query in click function

$(this).off('hover');
Answer:4

I'm trying to retrieve a single result from a multi-dimensional array and then push that result into each object contained within an object array. Here is my code; var data = { "questions": ["...

I'm trying to retrieve a single result from a multi-dimensional array and then push that result into each object contained within an object array. Here is my code; var data = { "questions": ["...

  1. push property into object javascript
  2. push property into object
  3. push new property into object javascript
  4. javascript push property into array
  5. javascript push object property into array

I created a chart using the dimple.js library with the following code: var myChart = new dimple.chart(svg, data); var x = myChart.addMeasureAxis("x", "x-measure"); x.tickFormat = "%"; var y = myChart....

I created a chart using the dimple.js library with the following code: var myChart = new dimple.chart(svg, data); var x = myChart.addMeasureAxis("x", "x-measure"); x.tickFormat = "%"; var y = myChart....

I have a JavaScript associative array, say: var myArray = new Object(); myArray["id1"] = "completed"; myArray["id2"] = "notCompleted"; myArray["id3"] = "started"; How can I update value of each item ...

I have a JavaScript associative array, say: var myArray = new Object(); myArray["id1"] = "completed"; myArray["id2"] = "notCompleted"; myArray["id3"] = "started"; How can I update value of each item ...

  1. update each value in dictionary python
  2. php update each value in array
  3. c# update each value in dictionary
  4. python update each value
  5. sql update different value each row
  6. loadrunner update value on each occurrence
  7. c# update value of each item in list

How can i stop an request which is already being processing in backend ? I am using EXT Js Framework,There are two button , 1.Search button This button queries the back end and fetches the ...

How can i stop an request which is already being processing in backend ? I am using EXT Js Framework,There are two button , 1.Search button This button queries the back end and fetches the ...