Get the name (type) of the event that was fired (triggered)
45,255
Solution 1
event.type
will get you what you want.
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" />
Solution 2
@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);
});
Author by
Marcio Mazzucato
Web developer interested in the following technologies: PHP, PostgreSQL and JQuery
Updated on September 04, 2020Comments
-
Marcio Mazzucato over 3 years
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"?
-
tkone about 12 yearsyou've got a typo in your HTML:
<input type
not<input tpys
:) -
Brave Dave over 11 yearsStrangely and unfortunately however the event.type for "focus" is "focusin", not "focus". Not sure if there are similar quirks with other events or not.