JQuery Tutorial

Define jQuery

How to use jQuery

jQuery Methods

Exercise Methods

jQuery Selectors

jQuery Events

Manipulate CSS Style

jQuery Traversing

jQuery Animations

jQuery AJAX

jQuery Plugins

jQuery Assignment

jQuery Events

An event is an action that may be handled by the software. Computer events can be generated or triggered by the system or by the user. Typically, events are handled synchronously with the program flow. Common jQuery events include, mouse events, keyboard events, hardware device such as a timer.

jQuery Mouse Events

jQuery mouse hover() event

Example 1: jQuery hover() event

$(document).ready(function () {
  $("#p1").hover(function () {
    $(this).css("background","#faa"); //run on mouse over
  },
  function () {
    $(this).css("background","#ffa"); //run on mouse out
  });
});

jQuery mouse click() event

Example 2: Click on any heading to change its color using jQuery click() event.

$(document).ready(function() {
  $("p").click(function(){
    $(this).css("color","red");
  });
});

jQuery mouseover(), mouseout() event

Example 3: jQuery mouseover(), mouseout event

$(document).ready(function(e) {
  $("div:nth-child(2)").mouseover(function(){
    $("body").css("background","red");	
  })
  $("div:nth-child(2)").mouseout(function(){
    $("body").css("background","white");	
  })
});

Example 4: jQuery mouseover(), mouseout event

$(document).ready(function(e) {
  $("p").mouseover(function(){
    $(this).text("Welcome, user");	
  })
  $("p").mouseout(function(){
    $(this).text("Hello, Hover Me");	
  })
});

Example 5: jQuery mouseover(), mouseout event

$(document).ready(function(e) {
  $("div:nth-child(2)").mouseover(function(){
    $("body").css("background","red");	
  })
  $("div:nth-child(2)").mouseout(function(){
    $("body").css("background","white");	
  })
});

jQuery mouseup() event

$("h1").mouseup(function() { ... });

jQuery mouseenter() Event

$("h1").mouseenter(function() { ... });

jQuery mouseleave() event

The mouseleave event is triggered when the mouse pointer leaves the selected element.

$("h1").mouseleave(function() { ... });

jQuery focus() and blur() event

The focus() event is triggered when the mouse pointer is entered in a input box. The blur() event is triggered when the mouse pointer is leave from a input box.

Attach multiple events

You can also combine multiple events in single HTML element. In the below example, when the user enter the mouse on paragraph, color changed to lightgray, when user leave the mouse, color became lightblue, and when user click on the paragraph color changed to yellow.

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

jQuery unbind() event

The unbind method removes the specified event. The event will not be repeated. All events for the currently triggered event will be removed.

$(this).unbind(); 
$("*").unbind(); 
$("*").unbind("mouseleave"); 
//All mouseleave events will be removed. Mouseleave event will not be repeated.
Example: jQuery mouseenter(), mouseleave(), unbind() Event
$(document).ready(function() {
  $("h1").mouseenter(function() {
    $(this).css("background-color","red");
  });
  $("h1").mouseleave(function() {
    $(this).css("background-color","yellow");
    $("*").unbind("mouseleave");
  });
});