jQuery Events
Events are often triggered when a user interacts with a web page, such as when a link or button is clicked, text is typed into an input box or textarea, or a selection is made in a select box. a key is hit on the keyboard, the mouse pointer is moved, and so on. In some circumstances, such as page load and unload events, the browser may initiate the event.
jQuery extends the fundamental event-handling capabilities by providing event methods for most native browser events, such as ready(), click(), keypress(), focus(), blur(), change(), and so on.
The document $(document).ready() is an event used to safely transform a page using jQuery. The code in this event will be executed only after the page's DOM is complete i.e. When the document is ready to be updated.
You may use the jQuery ready() function to run JavaScript code when the DOM is ready, as seen below:
In general, events are classified into four types: mouse events, keyboard events, form events, and document/window events. The sections that follow will provide a quick overview of all of these events as well as associated jQuery methods one by one.
Mouse Events
When a user clicks an element or moves the mouse pointer, a mouse event is triggered. Here are some typical jQuery techniques for handling mouse events.
jQuery click() Method
The jQuery click() method adds an event handler function to the selected components for the "click" event. When the user clicks on the element, the associated function is called. The following example hides items on a page when they are clicked.
jQuery dblclick() Method
The jQuery dblclick() method adds an event handler function to the selected item for the "dblclick" event. When the user double-clicks on the element, the associated function is called. When double-clicking on items, the following example will hide them.
jQuery hover() Method
jQuery's hover() method attaches one or two event handler functions to selected elements, which are run when the mouse cursor enters and exits the element. The first function is called when the user moves the mouse cursor over an element, and the second function is called when the user moves the mouse pointer away from that element. The following example highlights <p>
elements when the cursor is placed over them; The highlighting is removed when the cursor is moved back.
jQuery mouseenter() Method
When the mouse enters an element, the jQuery mouseenter() method invokes an event handler function that is run. When you set a pointer to a <p> element, as in the following example, the class highlight is added.
jQuery mouseleave() Method
When the mouse leaves an element, the jQuery mouseleave() method fires an event handler code that is run. When you remove the pointer from the <p> element in the following example, the square highlight is removed.
Keyboard Events
When a user presses or releases a key on the keyboard, a keyboard event is triggered. Here are some typical jQuery techniques for handling keyboard events.
jQuery keypress() Method
When the browser receives keyboard input from the user, the jQuery keypress() method attaches an event handler function to the selected components (usually form controls). The following example will show a message when the keypress event is fired and how many times it is fired when you press a key on the keyboard.
jQuery keydown() Method
When the user first touches a key on the keyboard, the jQuery keydown() method attaches an event handler function to the selected item (usually a control) that is invoked. When you press a key on the keyboard, the following example will show a message indicating when and how many times the keydown event has fired.
jQuery keyup() Method
When the user releases a key on the keyboard, the jQuery keyup() method attaches an event handler function to the selected components (usually form controls). When you press and release a key on the keyboard, the keyup event is triggered and the number of times it is fired is displayed in the following example.
Form Events
When a form control receives or loses attention, or when the user modifies a form control value, such as by putting text into a text input or selecting any item in a pick box, a form event is called. Here are several popular jQuery ways for handling form events.
jQuery change() Method
When an input, textarea, or selection of an element changes, the jQuery change() method attaches an event handler code that is invoked. The following example will display an alert message when you select an option in the dropdown select box.
jQuery focus() Method
When a selected element (usually controls and form links) receives focus, the jQuery focus() method fires an event handler code that is run. The following example displays a message when the focus is on a text input.
jQuery blur() Method
The jQuery blur() method adds an event handler function for creating components such as inputs, textareas, and selects when a form element loses focus. The following example will show a message when a text input loses focus.
jQuery submit() Method
When a user tries to submit a form, the jQuery submit() method calls an event handler function attached to the form's elements, which is done. The following example will show a message based on the value you input when you try to submit the form.
Document/Window Events
Events are also generated when the page's DOM (Document Object Model) is ready, or when the user resizes or scrolls the browser window, among other things. Here are several frequently used jQuery techniques for dealing with such occurrences.
jQuery ready() Method
When the DOM is fully loaded, the jQuery ready() method calls a function. The following example will replace the paragraph text when the DOM structure is complete and ready to be changed.
jQuery resize() Method
When the browser window is resized, jQuery's resize() method attaches an event handler function to the window element, which is executed. The following example will display the current width and height when you try to resize the browser window by moving its corners.
jQuery scroll() Method
When the scroll position of an element changes, the jQuery scroll() method calls an event handler function attached to window or scrollable iframes and elements, which is done.
FAQs
jQuery events are actions or occurrences that happen on a web page, such as a button click, mouse movement, or form submission. jQuery provides a comprehensive set of event methods that allow you to attach event handlers to elements and execute code when events occur. Event handling in jQuery involves selecting the desired element(s) using selectors and then using event methods like click(), mouseover(), or submit() to bind functions to those events. jQuery events provide a convenient way to enhance interactivity and responsiveness in web applications by executing specific actions in response to user interactions or other events.
jQuery provides several methods to attach event handlers to elements. The most common method is to use the on() method, which allows you to attach one or more event handlers to selected elements. For example, $('button').on('click', function() { /* code to execute on button click */ }) attaches a click event handler to all <button> elements on the page. You can also use shortcut methods like click(), mouseenter(), or submit() to attach specific event handlers. Additionally, jQuery provides delegated event handling using the on() method, which allows you to attach event handlers to elements that may be added dynamically to the page. By attaching event handlers to elements, you can define the behavior and actions to be executed when the specified events occur.
In jQuery, you can prevent the default behavior of an event and stop it from propagating further through the DOM tree. To prevent the default behavior of an event, you can use the preventDefault() method within the event handler. For example, event.preventDefault() can be called within a click event handler to prevent a link from navigating to its default URL. To stop the event propagation, you can use the stopPropagation() method. This prevents the event from triggering any additional event handlers on parent elements. For instance, event.stopPropagation() can be used within a click event handler to prevent the event from bubbling up to higher-level elements. By utilizing these techniques, you can control the behavior of events and customize the interaction experience in your web application.