views:

23

answers:

1

In javascript, we have event.srcElement that gives us the element on which some event is occured. Is there any way by which we can get this object in jQuery.

Also,

function myFun(){
  alert ( $(this) ); // what do this object represent? Is it what i need ?
}
+4  A: 

Short answer: If you're inside an event handler then yes, this is what you care about most of the time.


Inside an event handler this refers to what you're after or if needed access the event object passed in, for example:

$(".element").click(function(e) {
  //this is the element clicked
  //e.target is the target of the event, could be a child element
});

For example if your click handler was actually on a parent and you clicked something inside, the anchor in this case:

<div id="parent"><a href="#">Test</a></div>

With this handler:

$("#parent").click(function(e) {
  //this is the <div id="parent"> element
  //e.target is the <a> element
});
Nick Craver