Events play a critical role in the way websites are developed. Most web applications use events to react to user input, communicate with web servers and maintain a consistent user interface. jQuery is a popular JavaScript library used for registering and triggering events. It also provides default event handlers to respond to common event types. This makes it much easier to create functional web applications.
A computerized event-based system responds to different inputs and generates output based on predetermined rules. It usually communicates with other system components and performs tasks at specific times or intervals.
For example, the system that runs your car normally runs its engine at a consistent speed and timing. However, when you press the gas pedal, the car's engine speed increases. This is an example of an event triggering a handler; it determines how your car responds to your actions.
Events are triggered by different external influences such as network communications or programmatic user actions. These triggers initiate scripts that perform tasks based on predetermined rules.
For example, when a user clicks on a menu item, an event handler in the menu's code responds by displaying a different menu item.
An event can also notify you of errors or other unexpected behavior when initiating code execution in response. This allows you to swiftly identify and correct any issues before they negatively impact your application's performance or functionality.
Since events are initiated by external forces, they're always contextual and variable based on the situation. For this reason, jQuery provides various default event handlers for common scenarios such as mouse clicks and keyboard inputs. You can also write your own custom event handlers as needed for your application's unique needs. Using this approach, you can easily create interactive web pages that respond seamlessly to various user inputs. This reduces user interfaces from clunky popups or menus with dozens of options to only those relevant to the current situation.
Developers often use events for implementing quick responses and enhancing page load speeds. Many users click on links in web pages without meaning to do so- which initiates an event handler in the browser window code. This causes the URL of the linked website to open in a new tab or window instead of the current one. Additionally, registering event handlers ahead of time saves time when performing repetitive tasks such as form validation or AJAX callbacks. This makes development much faster without reducing quality due to fatigue from manually performing slow loops over and over again with callback functions.
Events are an easy way to implement interactive web pages without writing complex code or customizing browser settings each time something interesting occurs on a webpage. They allow you to easily register and trigger various events without worrying about how they affect the currently open webpage window. Additionally, using events helps enhance page load times by automating repetitive tasks such as form validation and loading external JavaScript files into the page window itself.
The events propagate from the element where they have been produced, for example a mouse click, up the html DOM until they find, in case there is one, a handler.
If they meet that handler, they execute it and follow their way up until they reach the top of the DOM.
Step by step what happens is (in a simplified way):
We have two possibilities to capture and react to events:
There are mainly two, the functions .on() and .one(). Both are analogous functions and the difference is that .one() executes the handler at most once and then deactivates it.
In both I indicate the name of the event that I want to capture and I can have a direct association of the handler to the event or a delegated association.
The operation is analogous to the capture with .on() or .one() but the "important" events have their own dedicated functions. This type of capture does NOT allow me to DELEGATE ASSOCIATION.
Among the most prominent named events:
The event object has many properties and methods. The most commonly used are the following, where e is the event:
The above-mentioned events can also be classified according to operation:
There are mainly three of them:
The connection of handlers to elements we had already seen at the beginning of the article using methods like .on() and the event named methods like $().click().
To disconnect those Handler jQuery mainly provides us with the .off() function that can be used in several ways and does the opposite effect of .on(). It can be used in several ways and some of the most common ones we will illustrate below:
$("some_selector").off("event1 event2...eventN");
Try it Yourself
Sometimes it may be necessary to simulate the occurrence of an event by artificially triggering it when I am interested.
This is achieved with the following functions:
To create my own events I will use the functions .on() and .trigger() and .triggerHandler().
$("selector").on("my_event"..........);
$("........").trigger("my_event");
$("........").triggerHandler("my_event");
Try it Yourself
In the following web practice many of the jQuery functions explained in this article:
Tips on SEO and Online Business
Previous Articles