Search results
1 sty 2014 · You can get the target of the event with e.target. However keep in mind that some browsers consider text nodes to be a target, so try something like this: var t = e.target; while(t && !t.id) t = t.parentNode; if( t) { alert("You clicked element #"+t.id); } This will find the first element that actually has an ID. Happy New Year!
The target property returns the element on which the event occurred, opposed to the currentTarget property, which returns the element whose event listener triggered the event. See Also: The currentTarget Property
27 mar 2022 · To get the id attribute of a DOM element on click, create an event listener using AddEventListener() and retrieve the id of the clicked object using Element.target().
5 mar 2024 · Use the event.target property to get the element the user clicked on. Use the parentElement property to get the parent and access its id property. Here is the HTML for the examples.
20 mar 2021 · We call window.addEventListener to attach the click event listener to the html element. Then we get the element that’s clicked on from the event.srcElement property. And we can get the ID of that element with the id 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. The following example will display the name of the element that was just clicked.
You can get an element by its ID by calling document.getElementById. It will return an element node if found, and null otherwise: var x = document.getElementById("elementid"); // Get the element with id="elementid".