'How do I add a class to a div in HTML using jQuery?

I want to use jQuery to add a class named container-c-ml to my HTML code. I have the following code below:

<div id="container">
<button>This is a button</button>
</div>

I have my HTML doc linked to a JavaScript file and I have jQuery added via the <head> tag of my doc. What should I do?



Solution 1:[1]

You can use the following jQuery code:

$(document).ready(function() {
  $("#container").addClass("container-c-ml");
});

Good luck with further learning to code!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1