0

how to change a::before style by click in jquery?

$("li").click(function () {
 $(this).children('a').css('color','red');
 $(this).children('a::before').css('color','green');
});
li > a::before{
 content: "Hello";
padding:5px 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li><a>World</a></li>
<li><a>World</a></li>
<li><a>World</a></li>
</ul>

each item has ::before in css file i want to change style them by click

user3668456
  • 141
  • 1
  • 9

1 Answers1

0

Unfortunatly it's not possible to use pseudo-elements in js You should use addClass and in your css add .myclass:before

$("li").click(function () {
 $(this).children('a').css('color','red');
 $(this).children('a').addClass('customClass');
});

li > a::before{
 content: "Hello";
 padding:5px 10px;
}

li > a.customClass::before{
 color: green;
}