I am back again with another JavaScript DOM question. Avoiding jQuery.
I am trying to select all elements of the same class in an HTML table and highlight them as blue on mouseover. I would like to highlight all the cells that have the same classname as the hovered element. So far, I have come up with two solutions to this problem and neither are working.
Solution One:
var cellElements = document.getElementsByTagName('td');
for (var i = 0; i < cellElements.length; i++){
cellElements[i].addEventListener('mouseover', function(e){
var hoveredElement = e.target;
if (cellElements[i].className === hoveredElement.className){
cellElements.style.color = "blue";
}
else {
cellElements.style.color = "black";
}
})
}
or Solution Two:
var cellElements = document.getElementsByTagName('td');
for (var i = 0; i < cellElements.length; i++){
cellElements[i].addEventListener('mouseover', function(e){
var hoveredElement = e.target;
document.getElementsByClassName(hoveredElement.className).style.color="blue";
})
}
HTML Snippet:
<tr class="row-c tr-selected">
<td><input type="checkbox"></input></td>
<td class="column-a">Specification 3</td>
<td class="column-b">Specification 3</td>
<td class="column-c">Specification 3</td>
</tr>
<tr class="row-d tr-selected">
<td><input type="checkbox"></input></td>
<td class="column-a">Specification 3</td>
<td class="column-b">Specification 3</td>
<td class="column-c">Specification 3</td>
</tr>
I would like to try and figure out how this is done without jQuery.