🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

Understanding and Handling jQuery Events Quickly and Effectively

Reading time 4 min
Published Jan 3, 2016
Updated Oct 1, 2019

You may remember JavaScript is an event-based coding language. Basically, this means that events define the performance of the program. This category includes all the user interactions, such as mouse clicks, touch screen swipes, or pressing something on one's keyboard.

By using jQuery events are handled in a simple and effective matter. In this tutorial, we will cover how to assign a particular event to your page and pass a function to it, as well as toggle jQuery event handlers. Not only will you find this more convenient, but it will also save you a great deal of time.

jQuery Events: Main Tips

  • In JavaScript, events refer to the visitors actions on the webpage. They are registered by event listeners assigned to elements on the HTML DOM.
  • The purpose of jQuery event methods is to manipulate these registered behaviors in a more convenient and advanced way.
  • The event.result property returns the previous values of custom events.

Events Explained

In JavaScript and jQuery events can also be called user interactions. The term refers to an action of the user interacting with the browser. It is registered by an event listener, which can have functions assigned to specify how it reacts to the event.

Simple examples of jQuery events include:

  • Moving the mouse over an element
  • Clicking an element
  • Pressing a key

There can also be various specifics for how events are listened to and handled.

In the table below, all of the jQuery events are listed:

Form Events Document/Window Events Mouse Events Keyboard Events Touch Events
submit load click keypress tap
change resize dblclick keydown taphold
focus scroll contextmenu keyup swipe
focusin unload hover swiperight
focusout error mousedown swipeleft
blur mouseenter
select mouseleave
mousemove
mouseout
mouseover
mouseup
toggle

Note: jQuery touch events are only relevant when a mobile touch screen is used (in most cases, a smart phone or a tablet).

Syntax

Assigning jQuery event listeners to elements is especially useful, as using pure JavaScript is clunky and harder to read than the jQuery way.

The syntax for applying event methods is very similar to any other jQuery statement that uses methods. Here's a basic example of a jQuery event method application:

Example
$(document).ready(() => {
    $("div").click(function() {
        $(this).hide();
    });
});

In this example, $("div") refers to the selector, and click defines the jQuery event that triggers the action on click jQuery event. As you might remember from the selectors tutorial, $(document).ready() makes it so the action only begins when the page is fully loaded.

The on click jQuery event might be replaced by any of the events specified in the table above. When the specified event occurs on the selected object, the code block in the jQuery event method's definition will be executed. In the example below, you can see dblclick being used:

Example
$(document).ready(() => {
    $("div").dblclick(function() {
        $(this).hide();
    });
});

To attach one or more event handlers to a selected element, use jQuery .on()
method. See how it's used in an example below:

Example
$("div").on("click", function() {
   $(this).css("background-color", "red");
});

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

Toggling Handlers

The toggle method is set apart from the rest of the mouse events because it may seem a bit confusing without understanding how to use it.

This method assigns two event handlers which alternate whenever the event occurs. The syntax for this method is not complicated either - it includes two functions to be called on the event, which are separated by a comma.

See the example below. In it, the same button makes an element show and hide:

Example
$("button").click(() => {
    $("div").toggle();
});

jQuery Event Result

The event.result property returns the previous values of custom events. Look at this simple example implementing this property:

Example
$("button").click(() => {
    return "Hello there!";
});
$("button").click((event) => {
    $("input").val(event.result);
    $("p").html(event.result);
});

jQuery Events: Summary

  • In JavaScript, user interactions such as clicking or hovering on a certain element are called events.
  • To register these events, jQuery event listeners are assigned to HTML DOM elements.
  • Events fired by touching a mobile touch screen are called jQuery touch events.
  • jQuery event methods allow developers to easily and professionaly modify these registered behaviors.