Javascript DOM Event Bind and this

Description

Javascript DOM Event Bind and this

View in separate window


<html>
<head></head>
<body>
In-line event registration//  w  w  w  .ja va  2 s  . co m
<div>Hi, I am div #1</div>
<div>Hi.. I'm also a div.</div>
<div>Don't forget me!</div>
<script>
let divList = document.getElementsByTagName("div");
for (let i = 0; i < divList.length; i++) {
  divList[i].onmouseover = function() { this.style.backgroundColor = "yellow"; };
  divList[i].onmouseout = function() { this.style.backgroundColor = "transparent"; };
}
</script>
</html>



PreviousNext

Related