how can i change the color of ::after pseudo-element when i click the a tag?
this is my html code:
<div class="menu">
<div class="row" style="justify-content: center;">
<div class="menu-item">
<a href="" class="row" id="border" onclick="change(0)">
<span>home</span>
</a>
</div>
<div class="menu-item">
<a href="" class="row" id="border" onclick="change(1)">
<span>subjects <i class="fa-solid fa-angle-down"></i> </span>
</a>
</div>
<div class="menu-item">
<a href="" class="row" id="border" onclick="change(2)">
<span>expert solutions</span>
</a>
</div>
</div>
</div>
and this is my css code:
:root
{
--background-border-menu: red;
}
.menu
{
width: 328px;
background-color: blue;
}
.menu-item
{
height: 70px;
background-color: blanchedalmond;
margin:0 5px;
}
.menu-item a
{
position: relative;
height: 70px;
padding: 5px;
font-size: 0.875rem;
text-transform: capitalize;
color: #2e3856;
font-family: 'Pathway Extreme', sans-serif;
font-weight: 600;
}
.menu-item a::after
{
position: absolute;
content: " ";
width: 100%;
height: 0.3rem;
background-color: var(--background-border-menu);
bottom: 0;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
left: 0;
}
and this is my js code:
function change(i)
{
var item = document.querySelectorAll("#border");
item[i].style.setProperty("--background-border-menu" , "blue");
}
I want when I click on one of my items, only that item’s change the a::after background color
When I click, it changes, but I don’t know how to remove the previous item
3
Answers
I have written a small library to be able to create/modify/remove css rules. Here’s an example of how to use it to toggle a css variable (from
:root
).