JavaScript AddEventListener
var element = document.getElementById("#id");
element.addEventListener('click', function(){
console.log("click");
});
Innocent Ibis
var element = document.getElementById("#id");
element.addEventListener('click', function(){
console.log("click");
});
const element = document.querySelector(".class__name");
element.addEventListener("click", () => {
console.log("clicked element");
});
//Syntax
target.addEventListener(type, listener);
//Example
document.addEventListener("click", modifyText);
//HTML
<table id="outside">
<tr><td id="t1">one</td></tr>
<tr><td id="t2">two</td></tr>
</table>
//JavaScript
// Function to change the content of t2
function modifyText(new_text) {
const t2 = document.getElementById("t2");
t2.firstChild.nodeValue = new_text;
}
// Function to add event listener to table
const el = document.getElementById("outside");
el.addEventListener("click", function(){modifyText("four")}, false);
var element=document.getElementById("some_id");
var listener=element.addEventListener('click',function(event){
});
window.addEventListener("scroll", function(){
console.log('scrolling');
});
// The anwer of Innocent Ibis is wrong!!
// It should not have a # for the ID seens we are using getElementById
var element = document.getElementById("id");
element.addEventListener('click', function(){
console.log("click");
});