Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get class in javascript with colon syntax for making dynamic class like tailwind css

i want to make dynamic class for hover , focus ,check and others. but I can’t able to get those class format(hover:bg-sky-700) for using colon(:).it shows error in console. please provide an idea to make dynamic class like tailwinds. thank you.

<div class="single-bg bg_amber_5 hover:bg_green"></div>

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If your class name contains reserved characters(., :, # etc.) you need to escape it to avoid ambiguity using CSS.escape:

// be careful you don't want to escape the leading "." since it meant to be indicating a class selector
const element = document.querySelector(`.${CSS.escape('hover:bg_green')}`);
console.log(element);
<div class="single-bg bg_amber_5 hover:bg_green"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading