Skip to main content
X

Explore your training options in 10 minutes

A Beginner’s Guide to jQuery on()

Ryan Manchester - January 14, 2021


The jQuery on() method is a way to attach an event handler on an element. The element passed to the selector can be anything on the page. Most commonly, on() is a way to attach a click handler to a button. In practice, on() can attach any event handler to any selected element.

In this guide, we will cover the basic syntax for on() and see some practical examples in action. It is important to mention that jQuery on() accepts optional arguments that will not be covered here. We are going to stick to the required arguments and common usage. More about the optional arguments can be found here .

What is jQuery on()

The jQuery on() method provides a stable way to attach an event handler to a selected element. Once the handler is named, on() takes a callback function. A callback function is a function that is passed to a method that executes at a later time. For our purposes, the callback function is where something will happen.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

Let’s say we used on() to attach a click event handler on a button. Our callback function is where we will define what action will happen after the button is clicked.

on() jQuery Syntax

Now that we have an idea of how on() works, let’s look at its specific syntax. Remember in all jQuery, we start with selecting our element. Then we call the on() method to attach our event handler. The first argument passed to on() is the event handler name itself, and is followed by the callback function.

$('.btn').on('click', function() {
  alert("You clicked the button!")
})

Here, we are selecting our button, calling on() , and passing our required arguments. Notice the event handler name must be in quotations. In our callback function, we are displaying an alert with a message. This lets us know the callback function is working properly.

More about jQuery alert() can be found here.

jQuery on() Example

Let’s expand on our above button example. We now know how basic syntax works for jQuery on() . For our example, let’s do something more than display an alert message after the button is clicked.

Instead of the alert message, we can display new content after the button is clicked. To do this, we will be using the jQuery append() method inside of our callback function. jQuery append() attaches content passed as an argument to the end of the selected element. Read more about jQuery append() here .

To begin, we are rendering an HTML button and a <div> where we will append new content.

<button class="btn">
Click me!
</button>

<div class="message">

</div>

This will render just the button for now.

There’s our button that wants to be clicked! Now, we select it with jQuery and have a message displayed after it is clicked.

$('.btn').on('click', function() {
  $('.message').append('The button has been clicked.')
})

Our button is selected by its class name and is passed a click event handler. In our callback function, we are appending a message to our <div> with a class name of “message.”

Our on() method works! One thing to note about using jQuery append() here is that our message will be attached to the bottom of our <div> every time the button is clicked. Let’s take a look at a different example.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

In our next example, we will replace the button with new content after it has been clicked. This will mimic a more controlled user experience.

<div>
<button class="btn">
Click me!
</button>
</div>

Here, we have wrapped our button in a <div>. This will allow us to use jQuery html() and replace the button with a <p> tag containing text. Sometimes we only want users to click a button once and be directed to new content!

$('.btn').on('click', function() {
  $('div').html('<p>The button has been replaced</p>')
})

In a similar fashion as our previous example, we are selecting the button and using on() to attach a click handler. Inside our callback function, we are replacing the button HTML with a paragraph element containing new HTML content.

Once the button is clicked:

We have new content and no button! This is a robust way to guide the user experience in a more controlled way.

Conclusion

We have seen how jQuery on() can attach an event handler to an element in a few lines of robust code. After we learned the basic syntax, we looked at two examples of how jQuery on() can be used. It is flexible enough to be used in a specific way or in a more general way depending on the desired outcome.

As this guide has been an introduction to using jQuery on() , make sure to spend some time practicing. There are many uses waiting to be discovered as you progress toward event handler mastery!

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

Ryan Manchester

About the author: Ryan is a technical writer at Career Karma, where he covers programming languages, technology, and web development. The Texas native earned his Bachelor's of Music Composition from the University of North Texas. Ryan is currently pursuing further education in web development, aiming to graduate from Flatiron School with a certification in full stack web development. Since joining the Career Karma team in November 2020, Ryan has used his expertise to cover topics like React and Ruby on Rails.

Skip to main content