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"?
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.
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.
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.
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" />
@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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With