4

I have a div with onclick param:

<div onclick="some_function()"></div>

How can I access the event object inside the function? I need it to get event.target

function some_function() 
{
event = event || window.event;
var target = event.target || event.srcElement;
//does not work in IE and Firefox, but works in chrome
}

5 Answers 5

12

This way:

<div onclick="some_function(event)"></div>

function some_function(evt) 
{
    // do something with evt (the event passed in the function call)
}

Note that the argument name MUST be event in the function call. In the event handler you can use the name you want.

A live example: http://jsfiddle.net/davidbuzatto/KHyAb/

Sign up to request clarification or add additional context in comments.

Comments

3

If we work with event listeners, then you have in 'this' the html element and in 'event' the event object.

<a id="mya" href="#">Link</a>
var myA= document.querySelector("#mya");
myA.addEventListener("click", some_function);
function some_funtion() {
  //Here the html element is: this
  //Here the event object is: event
  //event.target is not always equal to this
}
<div id="mydiv">click here <span>then here</span></div>
var myDiv = document.querySelector("#mydiv");
myDiv.addEventListener("click", other_function);
function other_function() {
    alert(this === event.target);
}    

Comments

1

Use the event to get to the target. Beware of target vs currentTarget issues:

https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

   <div onclick="some_function(event)"></div>

   function some_function(event) 
   {
       // event.target is the DOM element that triggered the event
   }

5 Comments

He wants the event object, not the target.
As I see it..he wants the event to get to the target:)
this is not always the same as event.target
When you have elements nested inside the one that has the handler, this will always refer to the element with the handler, but event.target will refer to the most deeply nested element clicked. <div onclick="alert(this === event.target);">click here <span>then here</span></div>
1
<div onclick="some_function(this, event)"></div>
function some_function(elem, e) 
{
    //You have both: html element (elem), and object event (e)
    //elem is not always equal to e.target
}

<div onclick="alert(this === event.target);">click here <span>then here</span></div>

1 Comment

This is valid for Inline event handlers, as was asked. But if we work with event listeners, then you have in 'this' the html element and in 'event' the event object.
0

Using event and this, both

document.querySelector("div").addEventListener("click", function(event) {

  some_function(event, this);
  
}, false);

function some_function(currentEvent, currentObject) {

  alert("Object is: " + currentObject.nodeName);
  alert("Event is: " + currentEvent.type);
  
};
<div>Click Me</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.