Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the element clicked on?

How would you check to see which item was clicked in the document, given this code?

if ( document.addEventListener && document.attachEvent && document.fireEvent ) {     document.attachEvent( "onclick", function() {      // ...     }); } 
like image 610
Leila Hamon Avatar asked Jul 31 '12 13:07

Leila Hamon


People also ask

How do you get the ID of a clicked element in react?

To get the id of the element on click in React: Set the onClick prop on the element to a function. Access the id of the element on the currentTarget property of the event . For example, event.currentTarget.id returns the element's id .

How do you click an element in JavaScript?

The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.

How do I get the clicked event ID?

One way to let us get the ID of the element when we click on an element is to set the onclick property of each element object to the same click event handler function. document. getElementById('1').

How do you find the elements of an event listener?

The addEventListener() method You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements. i.e the window object.


1 Answers

// using e.srcElement or event.srcElement 

try this :) and see this http://www.quirksmode.org/js/events_advanced.html

if (document.addEventListener){     document.addEventListener("click", function(event){         var targetElement = event.target || event.srcElement;         console.log(targetElement);     }); } else if (document.attachEvent) {         document.attachEvent("onclick", function(){         var targetElement = event.target || event.srcElement;         console.log(targetElement);     }); } 
like image 150
blueiur Avatar answered Sep 21 '22 03:09

blueiur