.mouseenter() JQ Home  <<  Events  <<  .mouseenter()

Mouseenter event handler.

Description

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

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

Syntax

Signature Description
.mouseenter( )Trigger the mouseenter JavaScript event on the specified element.
.mouseenter( handler(eventObject) )Bind an event handler to the mouseenter JavaScript event.
.mouseenter( [eventData ,] handler(eventObject) )Bind an event handler to the mouseenter 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.

.mouseenter( ) Example  Events  <<  Top

Trigger the mouseenter JavaScript event on the specified element.

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

In the example below we show a new message in the 'div' element with an id of 'div1' every time the mouse enters the division. Notice how the event doesn't fire when the mouse enters the image element, as the mouseenter event only triggers its handler when the mouse pointer enters the element it is bound to, not any descendants.

When the mouse enters the division we trigger the $('#div1').mouseenter(function(){}) code which outputs the message.



$(function(){
  $('#div1').mouseenter(function () {
    $('#div1').append('<br><code>mouseenter</code> JavaScript event triggered.');
  });
});
a picture of curry

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

Bind an event handler to the mouseenter JavaScript event.

  • This signature is a shortcut for .on('mouseenter', 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' below is entered.

When the input is typed in, the mouseenter 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').mouseenter(addText);

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

a picture of curry

We will show a message here.

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

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

  • This signature is a shortcut for .on('mouseenter', 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 is entered.

When the mouse button enters the division, the mouseenter JavaScript event fires off the $('#div3').mouseenter({ param1: '#scrollspan2', param2: 'mouseenter 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: 'mouseenter 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').mouseenter({ param1: '#scrollspan2', param2: 'mouseenter 2 ', 
                         param3: '**JavaScript event triggered**<br>' }, addText2);

  function addText2(event) {
    $(event.data.param1).append( event.data.param2 + event.data.param3);
  }
});



a picture of curry

We will show a message here.

Related Tutorials

jQuery Advanced Tutorials - Lesson 2 - Keyboard & Mouse Events