i'm beginner on JS (totally amazed and also fucked hhh)
using on a crome-based browers, i wanna make a code that tells me the "id" of the DOM,
when the element is "clicked". i want to use it on any existing websites.
that may means (on my short imagination)...
after loading certain website on my web browser, i will popup the JS console line (ctrl+shift+j),
and i will copy&paste this code and run enter. (by this code, click-eventlister might be loaded.)
right then i will click on target element.
so, i want the script to tell me the "id" of clicked element, and the event listener would disapear.
i wonder... is it possible?
if possible, please give me some keywords to search and learn.
thank you that read until here. my question maybe somewhat silly. ;;;
You can try using this script:
window.onclick = event => {
console.log(e.target.id);
};
This will fire an event when you click somewhere. It will then get the id of whatever you clicked and log it to the console.
If you want the event listener to disappear, you can just clear the function after it has run.
window.onclick = event => {
console.log(event.target.id);
window.onclick = null;
};
window.onclick = event => {
console.log(event.target.id);
window.onclick = null;
};
.item {
width: 60px;
height: 60px;
background: red;
margin: 10px;
}
<div id="1" class="item"></div>
<div id="2" class="item"></div>
<div id="3" class="item"></div>
<div id="4" class="item"></div>
<div id="5" class="item"></div>