X

LATEST #JQUERY NEWS

How to Use jQuery’s .mouseover() Method


jQuery’s .mouseover() method is used to execute some jQuery code whenever an element is “moused-over” (or, as the action is perhaps more commonly referred to, hovered upon). Really, the concept isn’t much different from jQuery’s .hover() method or CSS’s :hover effect, and .mouseover() can be used more or less interchangeably with them.

The syntax for .mouseover() is similar to the syntax of many jQuery event methods. The mouseover is the trigger event, and the code that’s within mouseover’s callback function is the result of the mouseover taking place. As an example, let’s say that when the mouse is moved over an h1 element, you want the color to change to blue. Here’s how that would look:

$("h1").mouseover(function(){
   $(this).css("color", "blue");
})

The biggest difference between using jQuery to achieve this effect rather than CSS’s hover is that with jQuery it’s a lot easier to have a mouseover effect change the styling of an element other than the one that was hovered upon than it is in CSS. For example, let’s say we want all of the text in the p elements to turn blue when we mouseover that same h1 tag. Here’s what that code would look like:

$("h1").mouseover(function(){
   $("p").css("color", "blue");
})

In the example above, all we had to do to make the mouseover effect apply to an element other than the h1 is replace the “this” keyword with the element we wanted to apply the CSS change to (in this case, “p”). In CSS, achieving this same effect would be more complicated.

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...