👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
on () method is a way to attach an event handler to an element. The element passed to the selector can be anything on the page. Most often,
on () is a way to attach a click handler to a button. Basically,
on () can attach any event handler to any selected item.
In this guide, we’ll cover the basic syntax for
on () and see some practical examples in action. It is important to remember that jQuery
on () accepts optional arguments which will not be processed here. We will stick to the required subjects and current usage. More information on optional topics can be found here .
What is jQuery on ()
on () method provides a stable way to attach an event handler to a selected item. Once the handler is named,
on () accepts a callback function. A callback function is a function passed to a method that is executed later. For our purposes, the callback is where something is going to happen.
Let’s say we used
on () to attach a button click event handler. Our callback function is where we’ll define what action will occur after clicking the button.
on () jQuery Syntax
Now that we have an idea of ‚Äã‚Äãhow
on () works, let’s take a look at its specific syntax. Remember that in all jQuery, let’s start by selecting our element. We then call the
on () method to attach our event handler. The first argument passed to
over () is the name of the event handler itself and is followed by the callback function.
Here we select our button, call
on () and pass the required arguments. Note that the name of the event handler must be in quotes. In our reminder function, we display an alert with a message. This lets us know that the callback function is working correctly.