Reputation: 69
I am facing an issue in jquery , i want to add a css test
class in body tag.
My code
(function($) {
if($("#root").length){
$("#root").closest("body").addClass('co_queue_page'); //not working
}
})(jQuery);
<div class="row"> //react code
<div id="root">
<div>
<header>
<div class="container-fluid">...</div>
</header>
</div>
</div>
</div>
what should i do? some help me help?
Upvotes: 0
Views: 148
Reputation: 474
Please add the below code:
$("body").addClass("class_name");
Upvotes: 0
Reputation: 111
To select the <body>
element, using jQuery, you can use:
const element = $(document.body);
const element = $("body");
Then you can use .addClass()
to add your custom class dynamically, like so:
element.addClass("co_queue_page");
This can be also done without any jQuery, accessing the body DOM element through the document object:
const element = document.body;
element.classList.add("co_queue_page");
Vanilla JS fiddle working example
Upvotes: 0
Reputation: 11622
You don't need to use .closest()
method, there is only one tag in HTML document, just do it by selecting the <body>
directly:
(function($) {
if($("#root").length){
$("body").addClass('co_queue_page');
}
})(jQuery);
Upvotes: 1