[JavaScript] Checking the Event Type

What if you want to attach one handler to multiple events of different types? How can you check which event is triggered?

1. window.event.type

The “window.event.type” property returns the name of an event. It is IE-only.

document.getElementById("clickBtn1").onclick = onClicked;

function onClicked(e) {
  alert(window.event.type);
}

 

2. type

The standard event object also supports the “type” property.

document.getElementById("clickBtn1").onclick = onClicked;

function onClicked(e) {
  alert(e.type);
}

 

3. jQuery

jQuery’s event object provides the normalized “type” property.

$(document).ready(function () {
  $("#clickBtn1").click(onClicked);
});

function onClicked(e) {
  alert(e.type);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s