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
»

jQuery’s .children() Method

Just like in CSS, it’s possible to use jQuery to select the ...

article image
»

jQuery’s .click() Method

With jQuery, it’s super easy to make something happen as a result ...

article image
»

Using jQuery’s .resize() Method

jQuery’s .resize() method is a cool way to have an event ...

article image
»

How to Use jQuery to Search and Replace HTML

Have you ever made a mistake in your code or in your text and dreaded ...

article image
»

How to Use jQuery’s .mouseover() Method

jQuery’s .mouseover() method is used to execute some jQuery ...

article image
»

How to Use jQuery’s .blur() Method

jQuery’s .blur() method isn’t exactly what it sounds like ...

article image
»

jQuery 3.0: Fix url.indexOf is Not a Function Error

jQuery 3.0 is out, and if you started working with it and plan to ...

article image
»

Set/Get/Delete Cookies with jQuery

There are a lot of plugins you can use to set, get, and delete ...

Screen Shot 2016-11-26 at 12.05.09 PM
»

3 Best jQuery Video Gallery Plugins

Do you have a lot of videos that you’d like to embed or neatly ...