Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check which event fired the function with jQuery?

How can i check which event called my function on Javascript with Jquery?

Like i have:

var mycall= function() { 
    alert('Which Witch is Which?');
}

$(window).load(mycall);
$(window).resize(mycall);

I understand that i could pass a parameter to the function, still i'm interested in a way to do this without passing any parameters.

like image 624
Jonathan DS Avatar asked Mar 29 '12 19:03

Jonathan DS


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.

What triggered the event in jQuery?

jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.

How do you get the element that triggered an event?

Answer: Use the event. target Property You can use the event. target property to get the ID of the element that fired an event in jQuery. This property always refers to the element that triggered the event.


2 Answers

Use the type property in the event object:

var mycall = function(e) { 
  alert(e.type);
};
like image 172
Guffa Avatar answered Sep 19 '22 11:09

Guffa


Add an event to the function args and then event.type will give tell which event is triggered. See below,

var mycall= function(e) { 
    alert(e.type);
}
like image 35
Selvakumar Arumugam Avatar answered Sep 18 '22 11:09

Selvakumar Arumugam