I have a problem with addEventListener. When I click on third div tag it doesn't change background color and alert is not working also.
Here is my html structure:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="myscript.js"></script>
</head>
<body>
<p>Click the button to display an alert box:</p>
<div>Robert</div>
<div>Arthur</div>
<div>Procedure(Third)</div>
<div>Fourth</div>
<button onclick="myfunc()">Try it</button>
</body>
Here is javascript file:
var Ar = ["red", "yellow", "green", "blue"]
function getRandomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function myfunc() {
alert(getRandomInt(0,5));
}
function Group(handler) {
var R = [];
for (var I = 0, E = document.getElementsByTagName('div'), L = E.length; I < L; I++) {
handler && handler.call(E[I]);
if (!handler)
R.push(E[I]);
}
return R;
}
//get third div tags and set a handler
Group()[2].addEventListener("click", handler, false);
function handler() {
alert("Test!");
Group()[2].style.backgroundColor = Ar[getRandomInt(0,3)];
}