I'm trying to highlight certain div element when mouse hovers over it with spesific class. I want the whole div with class Test to be highlighted on mouse over and none of the children in the div (the span) or the div with class Main. Currently it highlights anything inside the div on mouse over as well.
var linkit = document.getElementsByClassName("Test");
for (var i = 0; i < linkit.length; i++) {
linkit[i].addEventListener("mouseover", function (event) {
event.target.style.backgroundColor = "#ffcc00";
//some things I tried
//event.target.parentNode.style.backgroundColor = "#ffcc00";
//event.target.childNodes[0].style.backgroundColor = "#ffcc00";
//there will be more stuff in here, triggered by the event
});
}
var linkit = document.getElementsByClassName("Test");
for (var i = 0; i < linkit.length; i++) {
linkit[i].addEventListener("mouseout", function (event) {
event.target.style.backgroundColor = "transparent";
//some things I tried
//event.target.parentNode.style.backgroundColor = "transparent";
//event.target.childNodes[0].style.backgroundColor = "transparent";
//there will be more stuff in here, triggered by the event
});
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div style="position: absolute; left: 540px; top: 0px;">
<div class="Main">
<div class="Test Test0">
<span>Text0</span>
<span>T</span>
<span>A</span>
</div>
<div class="Test Test1">
<span>Text1</span>
<span>T</span>
<span>A</span>
</div>
<div class="Test Test2">
<span>Text2</span>
<span>T</span>
<span>A</span>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</body>
</html>
Thanks for any help!