Create and return the class name to be added - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:addClass

Description

Create and return the class name to be added

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").addClass(function(n){
            return "p_" + n;
        });//w  w w. j ava  2  s  . co m
    });
});
</script>
<style>
.p_0 {
    color: blue;
}
.p_1 {
    color: red;
}
.p_2 {
    color: yellow;
}

</style>
</head>
<body>

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

<h1>This is a heading</h1>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>



</body>
</html>

Related Tutorials