(Javascript) I stole this very nice code from another post here.
It changes the style.backgroundColor
of those div
elements with .onmouseover
.
<div id='idEl' class='classEl'>1</div>
<div id='idEl' class='classEl'>2</div>
<div id='idEl' class='classEl'>3</div>
<div id='idEl' class='classEl'>4</div>
<div id='idEl' class='classEl'>5</div>
<script>
var div = document.getElementsByTagName("div");
for(var i = 0; i < div.length; i++) {
div[i].onmouseover = function() {
this.style.backgroundColor = "green";
}
</script>
This works, but instead of doing it with TagName
(that would mess up all of my other dozens of div
), I would like to make it work with id
(if it's even possibile) or with className
.
And without using the html attributes
, all should be done through object properties
inside the <script>
.
It would be great if even the mighty addEventListener
could work.