jQuery API

.hover()

Contents:

.hover( handlerIn(eventObject), handlerOut(eventObject) ) Returns: jQuery

Description: Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.

  • version added: 1.0.hover( handlerIn(eventObject), handlerOut(eventObject) )

    handlerIn(eventObject)A function to execute when the mouse pointer enters the element.

    handlerOut(eventObject)A function to execute when the mouse pointer leaves the element.

The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element.

Calling $(selector).hover(handlerIn, handlerOut) is shorthand for:

$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

See the discussions for .mouseenter() and .mouseleave() for more details.

Examples:

Example: To add a special style to list items that are being hovered over, try:

<!DOCTYPE html>
<html>
<head>
  <style>
  ul { margin-left:20px; color:blue; }
  li { cursor:default; }
  span { color:red; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
    <li>Milk</li>
    <li>Bread</li>
    <li class='fade'>Chips</li>

    <li class='fade'>Socks</li>
  </ul>
<script>
$("li").hover(
  function () {
    $(this).append($("<span> ***</span>"));
  }, 
  function () {
    $(this).find("span:last").remove();
  }
);



//li with fade class
$("li.fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});

</script>

</body>
</html>

Demo:

Example: To add a special style to table cells that are being hovered over, try:

$("td").hover(
  function () {
    $(this).addClass("hover");
  },
  function () {
    $(this).removeClass("hover");
  }
);

Example: To unbind the above example use:

$("td").unbind('mouseenter mouseleave');

.hover( handlerInOut(eventObject) ) Returns: jQuery

Description: Bind a single handler to the matched elements, to be executed when the mouse pointer enters or leaves the elements.

  • version added: 1.4.hover( handlerInOut(eventObject) )

    handlerInOut(eventObject)A function to execute when the mouse pointer enters or leaves the element.

The .hover() method, when passed a single function, will execute that handler for both mouseenter and mouseleave events. This allows the user to use jQuery's various toggle methods within the handler or to respond differently within the handler depending on the event.type.

Calling $(selector).hover(handlerInOut) is shorthand for:

$(selector).bind("mouseenter mouseleave", handlerInOut);

See the discussions for .mouseenter() and .mouseleave() for more details.

Example:

Slide the next sibling LI up or down on hover, and toggle a class.

<!DOCTYPE html>
<html>
<head>
  <style>
  ul { margin-left:20px; color:blue; }
  li { cursor:default; }
  li.active { background:black;color:white; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
    <li>Milk</li>
    <li>White</li>
    <li>Carrots</li>
    <li>Orange</li>
    <li>Broccoli</li>
    <li>Green</li>
  </ul>
<script>
$("li")
.filter(":odd")
.hide()
 .end()
.filter(":even")
.hover(
  function () {
    $(this).toggleClass("active")
      .next().stop(true, true).slideToggle();
  }
);


</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .hover() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .hover()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://www.esthezia.net/ esthezia

    it would also be great to have a method something like $(“div”).isBeingFocused() which would return true/false if an element is currently being hovered or not.

    is there anything like this?

    • Anonymous

      var hover = $(“div”)
      .hover(
      function(){return true},
      function(){return false});

      This could do it?

      • Ali

        Neosun, esthezia, that last code example won’t work.

        try:

        $(“div”).hover(
        function(){$(this).addClass(“my-hover-class”)},
        function(){$(this).removeClass(“my-hover-class”)}
        );

        var isHovering = $(“div”).is(“.my-hover-class”);

        alert(“is hovering over div?: ” + isHovering);

        • http://www.esthezia.net esthezia

          thanks Ali!

          i haven’t tried it, but i think that should work.
          i needed this for a project long time ago – i thought (and still think) it would be very useful for a way of making a dropdown menu, as far as i remember. back then, i did it in some other way.

          thank you again

        • http://www.tourabout.com Mark

          Hi Ali – that's works a treat – just what I was looking for…

    • http://thatsmith.com/ Chad Smith

      I needed this for a project today so I made a plugin. See jQuery.isHovered()

      http://plugins.jquery.com/project/ishovered

  • http://www.myportal.no/ Thomas Svensson

    But how do I remove the hover() functionality from the elements I’ve added it to?

    • Zlatev

      hover() is a shortcut for .mouseenter().mouseleave(), so you should .unbind() them both like .unbind('mouseenter').unbind('mouseleave')

  • http://www.ciftcioglumobilya.com Ciftcioglumobilya

    * Added a Last-Modified header to the output of stylesheet= pages in order to reduce load with 304 Not Modified headers.
    * Added the ability to change the case of Gallery Category names without changing the spelling. http://www.ciftcioglumobilya.com
    * Modified Publish page title focus to only occur for new entries.

  • Tsang Kuen

    [code]$(function(){
    $("#enquiry, #email").hover( function(){
    $(this).addClass("hilite");
    }, function(){
    $(this).removeClass("hilite");
    });
    }); [/code]
    I don't see what I'm doing wrong, this only applies to the first object #enquiry and not #email

  • M1k3y02

    hover is a really fun, but how about binding it to selectors having specific class only ? how can you do that ?

  • Halarcon_g

    I declare a div without content but with a defined width and height in order to show some info. I have no problems with the hover effect in mozilla or opera but in IE8, the info appears and instantly appears and disappears and there is no time to read it i also add a
    or a   but this doesn't work. It works if i apply a background to the div. Is this a bug??

    It's this a bug??

  • Utyg

    The thing which I really don't understand is how one should pass data through the event.data mechanism in this case? Is this possible?

    • http://www.learningjquery.com/ Karl Swedberg

      No, not possible. You should use .bind() instead. In jQuery 1.4.4+, you can pass event.data with .mouseenter() and .mouseleave() too.

  • http://qrayg.myopenid.com/ Craig

    Is there a reason why the filter() portion of this code does not trigger in jQuery 1.4.3+? Does filter not work on hover anymore?

    $(document).ready(function(){
    $('#list a').hover(function(){
    $(this).addClass('on');
    }).filter(':first').hover();
    });

    • http://www.learningjquery.com/ Karl Swedberg

      hover isn't a real event, so you can't trigger it that way. Do you want to add a class on mouseenter or mouseleave or both? I'm guessing it's mouseenter (or, possibly, mouseover). If so, replace “hover” with “mouseenter” and it should work.

  • Artem

    Hi. I need help.
    I have moving slider with 3 images and I use buttons control (play|stop).
    I don't can get true code javascript, when it is have “.hover(function(){ });” …

    my code without .hover(function):
    var stopAnimation = function() {
    // Change the background image to “play”
    $(“#control”).css({ “background-image” : “url(images/slide_play.png)” });
    // Clear the interval
    clearInterval(interval);
    };

    plz help me!
    I need use image ” images/slide_play_hover.png ” for function hover.
    thx.

  • http://www.learningjquery.com/ Karl Swedberg

    hover isn't a real event, so you can't trigger it that way. Do you want to add a class on mouseenter or mouseleave or both? I'm guessing it's mouseenter (or, possibly, mouseover). If so, replace “hover” with “mouseenter” and it should work.

  • http://www.learningjquery.com/ Karl Swedberg

    No, not possible. You should use .bind() instead. In jQuery 1.4.4+, you can pass event.data with .mouseenter() and .mouseleave() too.

  • Cfddream

    i find jQuery hover function has a bug in ie6,
    if page is loading and your mouse mouseenter the target element

  • Tey Taghiyev

    I've been stuck on this problem ever since I've learned about this function. Say I do the following

    $('a').hover(
    function(){ $(this).animate({paddingLeft: '+=10'}, 200); },
    function(){ $(this).animate({paddingLeft: '-=10'}, 200); }
    );

    The idea is simple and common, hover on link = move right 10px, hover out = move back. The problem is if you click on the link, and DONT hover out. Just keep your mouse where it is. the new page loads, and when you go back in your browser the link element in still in it's shifted position. Basically, the hover-out never fires, and the browsers back button doesn't refresh the page, so the padding values don't reset. How do I get around this?? I swear I've searched everywhere for this issue.

    • Dominik

      Have you tried to add:
      $(window).unload( function () {} ); // clear ff and safari cache
      in your script?
      It forces firefox and safari to execute all scripts again when you click 'back' button.