jQuery Event Methods


JQuery is well-designed to respond to events on an HTML page.


What are Events?

All the actions of different visitors that the web page can respond to are called events.

An event represents the exact time when something happens.

Examples

  • moving a mouse over an element
  • selecting a radio button
  • clicking on an element

The word "fires / fire" is often used with events. Example: "A key press event is off, when you press a key".

Here are some common DOM events:

Mouse Events Keyboard Events Form Events Document/Window Events
click keypress submit load
dblclick keydown change resize
mouseenter keyup focus scroll
mouseleave   blur unload

jQuery Syntax For Event Methods

In jQuery, most DOM events have the same jQuery method.

To provide a click-through event for all categories on the page, you can:


$("p").click();

The next step is to define what should happen when an event is burning. You must transfer work to the event:


$("p").click(function(){
  // action goes here!!
});


Commonly Used jQuery Event Methods

$(document).ready()

The $(document).ready() method allows us to do the job once the document is fully loaded. This event has already been described in the jQuery Syntax chapter.

click()

The click() method pastes the event host function into the HTML component.

Task is performed when the user clicks on the HTML object.

The following example is: If a click event burns in the <p> element; hide <p> current section:


Example
$("p").click(function(){
  $(this).hide();
});

dblclick()

The dblclick() method attaches the event host function to the HTML component.

Task performed when user double-clicks HTML object:


Example
$("p").dblclick(function(){
  $(this).hide();
});

mouseenter()

The mouseenter() method attaches an event host function to the HTML component.

Task performed when mouse cursor enters HTML object:


Example
$("#p1").mouseenter(function(){
  alert("You entered p1!");
});

mouseleave()

The mouseleave() method attaches the event host function to the HTML component.

Task performed when mouse cursor leaves HTML element:


Example
$("#p1").mouseleave(function(){
  alert("Bye! You now leave p1!");
});

mousedown()

Mousedown() method attaches the event host function to the HTML component.

The function is performed, when the left, middle or right mouse button is pressed down, while the mouse over the HTML element:


Example
$("#p1").mousedown(function(){
  alert("Mouse down over p1!");
});

mouseup()

The mouseup() method attaches an event handler function to an HTML element.

The function is executed, when the left, middle or right mouse button is released, while the mouse is over the HTML element:


Example
$("#p1").mouseup(function(){
  alert("Mouse up over p1!");
});

hover()

The hover() method takes two functions and is a combination of mouseenter () and mouseleave() methods.

The first function is performed when the mouse enters the HTML component, and the second function is performed when the mouse leaves the HTML element:


Example
$("#p1").hover(function(){
  alert("You entered p1!");
},
function(){
  alert("Bye! You now leave p1!");
});

focus()

The focus() method attaches an event host function to an HTML form.

Work is done when the form field focuses on:


Example
$("input").focus(function(){
  $(this).css("background-color", "#cccccc");
});

blur()

The blur() method attaches the event host function to the HTML form.

Work performed when form field loses focus:


Example
$("input").blur(function(){
  $(this).css("background-color", "#ffffff");
});


The on() Method

The on() method attaches one or more event holders to selected elements.

Paste a click-through event in the <p> section:


Example
$("p").on("click", function(){
  $(this).hide();
});

Attach multiple event handlers to the <p> section:


Example
$("p").on({
  mouseenter: function(){
    $(this).css("background-color", "lightgray");
  },
  mouseleave: function(){
    $(this).css("background-color", "lightblue");
  },
  click: function(){
    $(this).css("background-color", "yellow");
  }
});