jQuery Basic

jQuery Home jQuery Introduction jQuery Getting Started jQuery Syntax jQuery Selectors jQuery Events

jQuery Effects

jQuery Show/Hide jQuery Fade jQuery Slide jQuery Animation jQuery Stop jQuery Chaining jQuery Callback

jQuery manipulation

jQuery Get/Set jQuery Insert jQuery Remove jQuery CSS Classes jQuery Style Properties jQuery Dimensions

jQuery advanced

jQuery Traversing jQuery Ancestors jQuery Descendants jQuery Siblings jQuery Filtering jQuery Ajax jQuery Load jQuery Get/Post jQuery No-Conflict

jQuery Exercises

jQuery Practice Examples jQuery Quiz

jQuery References

jQuery Overview jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Misc jQuery Properties

jQuery Event Methods

All the different visitors' actions that a web page can respond to are called events. An event represents the precise moment when something happens.

Events are often triggered by the user's interaction with the web page, such as when a link or button is clicked, text is entered into an input box or textarea, selection is made in a select box, key is pressed on the keyboard, the mouse pointer is moved etc. In some cases, the Browser itself can trigger the events, such as the page load and unload events. And below are also events.

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

jQuery enhances the basic event-handling mechanisms by offering the events methods for most native browser events, 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 an equivalent jQuery method.

Create a click event to all paragraphs on a page:

$("p").click();

The next step is to define what should happen when the event fires. You must pass a function to the event:

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

Mouse Events

A mouse event is fired when the user click some element, move the mouse pointer etc. Here're some commonly used jQuery methods to handle the mouse events.

Commanally used in jQuery Event Methods

$(document).ready()

The $(document).ready() is an event that is used to manipulate a page safely with the jQuery. Code included inside this event will only run once the page DOM is ready i.e. when the document is ready to be manipulated.

The click() Method

The click() method attaches an event handler function to an HTML element. And the function is executed when the user clicks on the HTML element

Example : When a click event fires on a <p> element; hide the current <p> element

Example

$("p").click(function(){
  $(this).hide();
});
Try it Yourself

The dblclick() Method

The jQuery dblclick() method attach an event handler function to the selected elements for "dblclick" event. The attached function is executed when the user double-clicks on that element.

Example

$("p").dblclick(function(){
  $(this).hide();
});
Try it Yourself

The hover() Method

The jQuery hover() method attach one or two event handler functions to the selected elements that is executed when the mouse pointer enters and leaves the elements. The first function is executed when the user place the mouse pointer over an element, whereas the second function is executed when the user removes the mouse pointer from that element.

Example

$(document).ready(function(){ $("p").hover(function(){ $(this).addClass("highlight"); }, function(){ $(this).removeClass("highlight"); }); });
Try it Yourself

The mouseenter() Method

The jQuery mouseenter() method attach an event handler function to the selected elements that is executed when the mouse enters the HTML element.

Example

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

The mouseleave() Method

The mouseleave() method attaches an event handler function to an HTML element. The function will execute when mouse moves from paragraph after hover the mouse on paragraph.

Example

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

The mousedown() method

The mousedown() method attaches an event handler function to an HTML element. The function is executed, when the left, middle or right mouse button is pressed down, while the mouse is over the HTML element

Example

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

The mouseup() method

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!");
});
Try it Yourself

The focus() method

The focus() method attaches an event handler function to an HTML form field. The function is executed when the form field gets focus

Example

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

The blur() method

The blur() method attaches an event handler function to an HTML form field. The function is executed when the form field loses focus

Example

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

The on() method

The on() method attaches one or more event handlers for the selected elements.Attach a click event to a <p> element

Example

$("p").on("click", function(){
  $(this).hide();
});
Try it Yourself

The submit() method

The jQuery submit() method attach an event handler function to the <form> elements that is executed when the user is attempting to submit a form. The following example will display a message depending on the value entered when you try to submit the form.

Example

$(document).ready(function(){ $("form").submit(function(event){ var regex = /^[a-zA-Z]+$/; var currentValue = $("#firstName").val(); if(regex.test(currentValue) == false){ $("#result").html('<p class="error">Not valid!</p>').show().fadeOut(1000); // Preventing form submission event.preventDefault(); } }); });
Try it Yourself

The scroll() method

The jQuery scroll() method attach an event handler function to the window or scrollable iframes and elements that is executed whenever the element's scroll position changes.

The function will display a message when you scroll the browser window.

Example

$(document).ready(function(){ $(window).scroll(function() { $("p").show().fadeOut("slow"); }); });
Try it Yourself

Attach multiple event

Attach multiple event handlers to a <p> element:

Example

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

The change() method

The jQuery change() method attach an event handler function to the <input>, <textarea> and <select> elements that is executed when its value changes. The following example will display an alert message when you select any option in dropdown select box.

Example

$(document).ready(function(){ $("select").change(function(){ var selectedOption = $(this).find(":selected").val(); alert("You have selected - " + selectedOption); }); });
Try it Yourself
To learn more about Events, we shoud go to the Reference chapter Click..