Skip to content
Related Articles

Related Articles

Improve Article

Create a paragraph element with some text and append it to end of document body using jQuery

  • Last Updated : 31 Dec, 2020

In this article, we will create a paragraph element with some text and append it to the end of the document body using jQuery. To append some text at the end of the document body, we use add() and appendTo() methods. The jQuery add() method is used to add elements to the existing group of elements. This method can add elements to the whole document, or just inside the context element if the context parameter is defined.

Syntax:

$(selector).add(element, context_parameter)

The jQuery appendTo() is an inbuilt method that is used to insert an HTML element at the end of the selected element.

Syntax:

$(content).appendTo(selector)

Example:



HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
     
    <!-- Import jQuery cdn library -->
    <script src=
    </script>
  
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("button").add("
                    <p>Hello World!</p>")
                    .appendTo(document.body);
            });
        });
    </script>
</head>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to create a paragraph element with
        some text and append it <br>to the end
        of the document body using jQuery?
    </h3>
      
    <p>GeeksforGeeks computer science portal</p>
  
    <span>GeeksforGeeks</span>
    <br><br>
  
    <button>Click Here!</button>
</body>
  
</html>

Output:

Before Click Button:

After Click Button:

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :