Home > Net >  How do I add a class to a div in HTML using jQuery?
How do I add a class to a div in HTML using jQuery?

Time:04-25

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?

CodePudding user response:

You can use the following jQuery code:

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

Good luck with further learning to code!

  • Related