I'm using a mouse over event to have the shadows change from black to pink. However, when I add the mosue over event onto the other cards they only change the first card. How to I get each indivual card to change colour when hovered?
JS
<script>
function mouseOver() {
document.getElementById("CardHover").style.boxShadow = "-6px 6px 0px 0px #FF8A88";
}
function mouseOut() {
document.getElementById("CardHover").style.boxShadow = "-6px 6px 0px 0px #000";
}
</script>
HTML
<div class="report" id="CardHover" onclick="location.href='#';" onmouseover="mouseOver()" onmouseout="mouseOut()">
<img style="width: 100%; height: 100%; border-radius: 10px;" src="#.png" alt="">
<div style="display: flex; flex-direction: column; align-items: flex-start; gap: 6px; padding: 8px;">
<h6>#ServiceDesign #DesignThinking #Ethnography</h6>
<h4>Title</h4>
<p>Body Copy</p>
<a href="#" class="tertiary">Explore</a>
</div>
</div>
<div class="report" id="CardHover" onclick="location.href='#';" onmouseover="mouseOver()" onmouseout="mouseOut()">
<img style="width: 100%; height: 100%; border-radius: 10px;" src="#.png" alt="">
<div style="display: flex; flex-direction: column; align-items: flex-start; gap: 6px; padding: 8px;">
<h6>#ServiceDesign #DesignThinking #Ethnography</h6>
<h4>Title</h4>
<p>Body Copy</p>
<a href="#" class="tertiary">Explore</a>
</div>
</div>
Expectng to have each card have it's own hover using mosue over events.
The main issue in your code is because you're repating the same
idattribute value on all the card elements. This is invalid asidmust be unique. To fix that problem change theidto aclass.That being said, there's quite a few other problems in the code which need addressing:
onXattributes in your HTML. The former allow you to get a reference to the element that raised the event from the Event object, so that you can update it as required.mouseenter/mouseleaveand notmouseover. The latter fires once for every pixel the mouse moves over the target element, which is overkill in this use case.clickevent handlers that cause page redirects on elements which cannot accept a click event by default - eg. adiv. This is because it's very poor for accessibility.With all that said, here's an updated working implementation:
However, the above is almost entirely moot because you should be using CSS to update the UI, not JS. Here's how to do that using the
:hoverpseudo-selector: