Code has been added to clipboard!

Event Data: Learn to Apply Properties to Event Objects

Reading time 2 min
Published Dec 29, 2017
Updated Oct 9, 2019

Event Data: Main Tips

  • jQuery event objects are to be assigned to event handler jQuery. You can apply various event properties for consistent user experience.
  • event.data property refers to the optional event data, passed to the event method.
  • You may also use event.isDefaultPrevented, event.isImmediatePropagationStopped, event.isPropagationStopped, and event.namespace properties.
  • These properties check whether specific methods are applied to elements, and return jQuery namespaces after events are invoked.

event.data

The event.data property returns the optional data parameter value, defined with the event method. In the example below, you can see click event method is used, and different values show up as you click various elements:

Example
$("p").each(function(i) {
   $(this).on("click", {x:i}, function(event) {
       alert("Clicked: " + $(this).index() + event.data.x);
   });
});

event.isDefaultPrevented

The event.isDefaultPrevented checks whether the preventDefault() method was used on a specific event object. See the JavaScript alert pop up to display the answer in the example below:

Example
$("a").click((event) => {
    event.preventDefault();
    alert("Is it true that preventDefault() was called? " + event.isDefaultPrevented());
});

event.isImmediatePropagationStopped

The event.isImmediatePropagationStopped method checks whether the event.stopImmediatePropagation() was ever used on the event object. The alert box in the example below will return a boolean value as you click the element:

Example
$("p").click((event) => {
    event.stopImmediatePropagation();
    alert(event.isImmediatePropagationStopped());
});

event.isPropagationStopped

The event.isPropagationStopped method checks whether the event.stopPropagation() method was used on the event object. In the example below, you will again see an alert when you click the element:

Example
$("div").click((event) => {
    event.stopPropagation();
    alert(event.isPropagationStopped());
});

event.Namespace

The event.namespace property returns the specified jQuery namespace when the event gets triggered. In the example below, it pops up as an alert:

Example
$("input").on("custom.sampleNamespace", (event) => {
    alert(event.namespace);
});
$("input").click(function(event) {
    $(this).trigger("custom.sampleNamespace");
}); 
$("button").click(() => {
    $("input").off("custom.sampleNamespace");
});
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