So my question is how to get an ID of an element that has just been clicked? (JavaScript)
Thank you for your answers!
You can use the target element (in all browsers except IE) and srcElement (in IE) in order to retrieve the clicked element:
function click(e) {
// In Internet Explorer you should use the global variable `event`
e = e || event;
// In Internet Explorer you need `srcElement`
var target = e.target || e.srcElement;
var id = target.id;
}
However be aware of event bubbling. The target
may not be the element you expect.
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