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() { // ... }); }
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 .
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.
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').
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.
// 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); }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With