MHasan
MHasan

Reputation: 69

adding css class in <body> javascript

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?

enter image description here

enter image description here

Upvotes: 0

Views: 148

Answers (3)

Akash prajapati
Akash prajapati

Reputation: 474

Please add the below code:

$("body").addClass("class_name");

Upvotes: 0

gdevdeiv
gdevdeiv

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");

jQuery fiddle working example

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

ROOT
ROOT

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

Related Questions