Skip to content
Related Articles

Related Articles

Improve Article

How to add a class to the last paragraph element using jQuery ?

  • Last Updated : 31 Dec, 2020
Geek Week

In this article, we will learn to add a class to the last paragraph element using jQuery. To add a class to the last paragraph element, we use last() and addClass() methods. The jQuery last() function is an inbuilt function that is used to find the last element of the specified elements.



The addClass() method is used to add more classes and their respective properties to each selected element. It can also be used to change the property of the selected element. Here, the class name can be used directly with the element which is going to be selected.





<!DOCTYPE html>
<html lang="en">
        .GFG {
            color: green;
            font-size: 24px;
            font-weight: bold;
    <!-- Import jQuery cdn library -->
    <script src=
        $(document).ready(function () {
            $("button").click(function () {
<body style="text-align: center;">
    <h1 style="color: green;">
        How to add a class to the last
        paragraph element using jQuery?
    <p>GeeksforGeeks computer science portal</p>
    <button>Click Here!</button>


Before Click the Button:

After Click the Button:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :