How to use HTML AddClass Method in JQuery

This article describe about HTML AddClass Method in JQuery.
  • 1640

HTML addclass() method in jQuery

The addclass method add a new class in the selected position.

Addclass method add class to the class attribute and does not remove the attribute.

Syntax

$(selector).addClass(function(index,oldclass))

Example

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function ()

{

        $("button").click(function ()

{

            $('p').addClass(function (n)

 {

                return 'par_' + n;

            });

        });

    });

</script>

<style type="text/css">

.par_0

{

color:blue;

}

.par_1

{

color:yellow;

}

</style>

</head>

 

<body  style ="background-color:Red">

<h1>This is a JQuery.</h1>

<p>This is a HTML.</p>

<p>This is addclass method.</p>

<button>Add classes to p elements</button>

</body>

</html>

Output1

op1.jpg

Output2

op2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.