so i have another JavaScript question how can I alter the following
Code: Select all
<script>
function hoveron(event) {
var target = event.target.parentNode;
target.style.cssText = "background-color:red";
}
function hoveroff(event) {
var target = event.target.parentNode;
target.style.cssText = "";
}
var cells = document.querySelectorAll("td");
for (i = 0; i < cells.length; i++) {
cells[i].addEventListener("mouseover", hoveron);
cells[i].addEventListener("mouseout", hoveroff);
}</script>
See if I have a table like this
Code: Select all
<table>
<tr>
<td>item 1</td>
<td>item 2</td>
</tr>
</table>
Now i know i could give each row an ID and then just do document.getElementById("id"); and that would probably do the trick but i feel like there has to be a cleaner way to do this there has to be some way to make the script always select the row closest to where the hover event happened.
i tried changing
Code: Select all
var target = event.target.parentNode
Code: Select all
var target = event.target.closest('tr');