What is the hasClass function with plain JavaScript

0 votes

How do you do jQuery’s hasClass with plain ol’ JavaScript? For example,

<body class="foo thatClass bar">

What’s the JavaScript way to ask if <body> has thatClass?

Sep 2, 2020 in Java-Script by kartik
• 37,520 points
1,058 views

1 answer to this question.

0 votes

Hello @Kartik,

Simply use classList.contains():

if (document.body.classList.contains('thatClass')) {
    // do some stuff
}

Other uses of classList:

document.body.classList.add('thisClass');
// $('body').addClass('thisClass');

document.body.classList.remove('thatClass');
// $('body').removeClass('thatClass');

document.body.classList.toggle('anotherClass');

Hope it helps!!

Thank You!!

answered Sep 2, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
0 answers

What is the difference between ' and " in JavaScript?

I came across this query and am ...READ MORE

Sep 23, 2022 in Java-Script by Abhinaya
• 1,160 points
598 views
0 votes
1 answer

What is the difference between node.js and io.js?

Hello, io.js: Node-forward is basically being merged into io.js forked on ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
846 views
0 votes
1 answer

What is an appropriate content-type header for JavaScript files?

Hello @kartik, JS has two registered MIME types:  The ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,840 points
2,637 views
0 votes
1 answer

How to access PHP var from external javascript file?

Hello @kartik, You don't really access it, you ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,840 points
7,760 views
0 votes
1 answer

How do I escape a single quote in SQL Server?

Hello @kartik, Single quotes are escaped by doubling ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,840 points
6,216 views
0 votes
1 answer

How to specify a port to run a create-react-app based project?

Hello @kartik, You could use cross-env to set the port, ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
5,861 views
0 votes
1 answer

How do you set a default value for a MySQL Datetime column?

Hello @kartik, In version 5.6.5, it is possible ...READ MORE

answered Aug 18, 2020 in PHP by Niroj
• 82,840 points
7,984 views
0 votes
1 answer

What is callback function in JavaScript?

callback is not a keyword, its just ...READ MORE

answered Jan 30, 2020 in Java-Script by Niroj
• 82,840 points
18,916 views
0 votes
1 answer

what is the $(document).ready equivalent without jQuery?

Hello @kartik, There are three options: If script is the last ...READ MORE

answered Apr 2, 2020 in Java-Script by Niroj
• 82,840 points
21,727 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP