jQuery Add Class

jquery jQuery Add Class Add Class By JQuery

How to add the class in the HTML tag using jQuery?

Sometime we need to add class under some condition on page run time. It is very simple in the jQuery to add the Class on the HTML tag. We can add the class in the HTML tag using jQuery function addClass().

Syntax of jQuery function addClass()

$('TagSelector').addClass('Class-Name'); Let's we make the CSS class with red color and with black background.

On click button add CSS class using jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <style type="text/css"> .my-class{ color:red; background-color: black; } </style> <button id="mybutton">Click To Add my-class to div</button> <div id="mydiv">Write Less ,Do More With jQuery</div> <script> $("#mybutton").click(function(){ $("#mydiv").addClass('my-class'); }); </script>

Output of jQuery functions addClass()

Write Less ,Do More With jQuery
jquery Add Class By JQuery Chapter Next »