How to check classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to check classes

How can I check if an HTML has certain class. Im trying with this Var x = ($("#pub").attr("class")).split(); If ("underline" in x) alert("y") But it ain't working, any tips?

24th Apr 2021, 12:08 AM
Rodrigo Tallar
Rodrigo Tallar - avatar
2 Answers
+ 3
With vanilla javascript var x = document.querySelector("#pub"); var hasClass = x.classList.contains("underline"); if (hasClass) { alert("It has"); } else { alert("It seems it doesn't"); }
24th Apr 2021, 2:27 AM
Krish
Krish - avatar
+ 1
it appear you're using Jquery this will help you https://www.w3schools.com/jquery/html_hasclass.asp
24th Apr 2021, 1:01 AM
Med Amine Fh
Med Amine Fh - avatar