showing results for - "js document addeventlistner"
Lena
06 Oct 2016
1//Syntax
2target.addEventListener(type, listener);
3//Example 
4document.addEventListener("click", modifyText);
5//HTML
6<table id="outside">
7  <tr><td id="t1">one</td></tr>
8  <tr><td id="t2">two</td></tr>
9</table>
10//JavaScript
11// Function to change the content of t2
12function modifyText(new_text) {
13  const t2 = document.getElementById("t2");
14  t2.firstChild.nodeValue = new_text;
15}
16// Function to add event listener to table
17const el = document.getElementById("outside");
18el.addEventListener("click", function(){modifyText("four")}, false);
19