.mouseover() JQ Home  <<  Events  <<  .mouseover()

Mouseover event handler.

Description

The .mouseover() method is used to bind an event handler to The JavaScript mouseover event or trigger that event on the specified element.

  • The mouseover event is sent to an element when the mouse pointer is moved over the element it is bound to.
  • The mouseover event is similar to the mouseenter event but the events differ in the way they handle event bubbling:
    • The mouseover event triggers its handler when the mouse pointer enters the element it is bound to, as well as any descendants.
    • The mouseenter event only triggers its handler when the mouse pointer enters the element it is bound to, not any descendants.

Syntax

Signature Description
.mouseover( )Trigger the mouseover JavaScript event on the specified element.
.mouseover( handler(eventObject) )Bind an event handler to the mouseover JavaScript event.
.mouseover( [eventData ,] handler(eventObject) )Bind an event handler to the mouseover JavaScript event, optionally passing an object of data.

Parameters

Parameter Description Type
handler( eventObject )A function to execute each time the event is triggered.Function
eventDataAn object of data to pass to the event handler.Anything

Return

A jQuery object.

.mouseover( ) Example  Events  <<  Top

Trigger the mouseover JavaScript event on the specified element.

  • This signature is a shortcut for .trigger('mouseover').

In the example below we show a new message in the 'div' element with an id of 'div8' every time the mouse enters the division. Notice how the event also fires when the mouse enters the image element, as the mouseover event triggers its handler when the mouse pointer enters the element it is bound to, as well as any descendants.

When the mouse enters the division we trigger off the mouseover JavaScript event on the 'div8'. This then fires off the $('#div8').mouseover(function(){}) code which outputs the message.


$(function(){
  $('#div8').mouseover(function () {
    $('#div8').append('<br><code>mouseover</code> JavaScript event triggered.');
  });

  $('#div8').mouseover();
});


a picture of curry

.mouseover( handler(eventObject) ) Example  Events  <<  Top

Bind an event handler to the mouseover JavaScript event.

  • This signature is a shortcut for .on('mouseover', handler).

In the example below we show a new message in the 'p' element with an id of 'scrollspan1' each time the 'div' element with an id of 'div2' or the image within it are left.

When the mouse leaves the division or image, the mouseover JavaScript event fires off the addText(event) mothod which outputs a message.

What we are doing here is passing across the event object to the function addText(event)method. The data we specify gets tagged onto the event.data property.



$(function(){
  $('#div2').mouseover(addText);

  function addText(event) {
    $('#scrollspan1').append('mouseover 1 **JavaScript event triggered**<br>');
  }
});



a picture of curry

We will show a message here.

.mouseover( [eventData ,] handler(eventObject) ) Example  Events  <<  Top

Bind an event handler to the mouseover JavaScript event, optionally passing an object of data.

  • This signature is a shortcut for .bind('mouseover', handler).

In the example below we show a new message in the 'p' element with an id of 'scrollspan2' each time the 'div' element with an id of 'div3' below, or the image within it, is entered.

When the mouse button enters the division or the image within it, the mouseover JavaScript event fires off the $('#div3').mouseover({ param1: '#scrollspan2', param2: 'mouseover 2 ', param3: '**JavaScript event triggered** ' }, addText2); code.

What we are doing here is passing across the event object to the function addText2(event). The map we specify, in our case { param1: '#scrollspan2', param2: 'mouseover 2 ', param3: '**JavaScript event triggered** ' } gets tagged onto the event.data property. We then access this parameter in the function via event.data.param and use it as part of the appended data.


$(function(){
  $('#div3').mouseover({ param1: '#scrollspan2', param2: 'mouseover 2 ', 
                         param3: '**JavaScript event triggered**  ' }, addText2);
  function addText2(event) {
    $(event.data.param1).append( event.data.param2 + event.data.param3<br>');
  }
});


a picture of curry

We will show a message here.


Related Tutorials

jQuery Advanced Tutorials - Lesson 2 - Keyboard & Mouse Events