Answers for "get dom element from click event"

2

javascript onclick get event

<div onclick="doSomething(event,this)"></div>
<script>
     function doSomething(e,el){
         e = e || window.event;
         console.log(e); //will be the event
         console.log(el); //we be the dom element 
     }
</script>
Posted by: Guest on November-30-2020
0

get element by click

window.onclick = e => {
    console.log(e.target);  // to get the element
    console.log(e.target.tagName);  // to get the element tag name alone
}
Posted by: Guest on April-08-2021
0

get element by click

window.onclick = e => {
    console.log(e.target.innerText);
}
Posted by: Guest on April-08-2021

Browse Popular Code Answers by Language