Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the name (type) of the event that was fired (triggered)

I have the following code:

$('#button').on('click change', function() {     alert('Who fired me, click or change?'); }); 

How can I know if the event called was "click" or "change"?

like image 707
Marcio Mazzucato Avatar asked Mar 15 '12 17:03

Marcio Mazzucato


People also ask

How do I find out which event was fired?

Open Google Chrome and press F12 to open Dev Tools. Go to Event Listener Breakpoints, on the right: Click on the events and interact with the target element. If the event will fire, then you will get a breakpoint in the debugger.

How can you tell if a event is triggered?

To check if event is triggered by a human with JavaScript, we can use the isTrusted property. to check if the event is triggered by a human in our event handler callback. e is the event object and it had the isTrusted property. If isTrusted is true , then the event is triggered by a human.

Which event is fired on a text field within a form when user clicks on it?

When a user clicks a button or presses a key, an event is fired. These are called a click event or a keypress event, respectively. An event handler is a JavaScript function that runs when an event fires.


2 Answers

event.type will get you what you want.

DEMO

See also: List of event types

$('#button').on('click change', function(){      console.log(event.type + ' is fired');  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  <input type="text" id="tipo-imovel" />
like image 169
Selvakumar Arumugam Avatar answered Sep 20 '22 15:09

Selvakumar Arumugam


@Vega solution is correct for simple events. However, if you namespace your events, (i.e. player.play) then you must get the namespace as well. For example, lets say I trigger the event:

$('#myElement').trigger('player.play'); 

Then to get the full event name, you need to do the following:

$('#myElement').on('player.play', function(e) {     console.log('Full event name: ' + e.type + '.' + e.namespace); }); 
like image 36
Troy Grosfield Avatar answered Sep 16 '22 15:09

Troy Grosfield