Code has been added to clipboard!

Learn to Add Event Listeners Using jQuery .on() Method

Reading time 1 min
Published Jan 9, 2018
Updated Sep 27, 2019

jQuery on: Main Tips

  • The jQuery .on() method adds event listeners to the selected element.
  • The .off() function removes the event listeners added by on().
  • For instance, the .on() method with the change as the event parameter works similarly to change().

Usage and Syntax

The .on() jQuery method is used to add event handlers:

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

This is the syntax you need to use:

$("selector").on(event,selector,data,handler);

jQuery .on() method takes four arguments:

  • event - an event type to specify what handlers should be added. Accepts multiple values separated by commas.
  • selector - a selector to filter which descendants can trigger the jQuery event handler.
  • data - additional data that gets passed to the handler via the event.data when the event triggers.
  • handler - the handler function that is executed when the jQuery on event is triggered.

Note: if the selector is undefined or null in the jQuery on method, the handler applies to all of the children of the selected elements.

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX