Open In App

How to insert a jQuery object after all paragraphs using jQuery ?

Last Updated : 22 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The task is to insert a jQuery object after all the paragraphs using jQuery.

Approach:

  • Create DOM element or jQuery object using <span> tag.
  • Create a paragraph using <p> tag.
  • Create a button using button tag after clicking on which the object will be inserted after paragraph.
  • Write a script that will call a function jQuery.after()  which is used to insert a jQuery object after all the paragraphs.

Example: Below is the working code using the above-mentioned steps.

HTML
<!DOCTYPE HTML>
<html>

<head>
  <script src=
"https://code.jquery.com/jquery-git.js">
  </script>
</head>

<body>
  <h1 style="color:green"> 
     GeeksforGeeks 
  </h1>

  <span>
     Dom element / jQuery object
  </span>

  
<p>
     This is paragraph
  </p>


  <button id="GFG_button1" style="display: block;
     margin-top: 15px;">
     Click here to insert object after paragraph
  </button>

  <script>
      $('#GFG_button1').click(function () {
          $("p").after($("span"));
      });
  </script>
</body>

</html>

Output:


Next Article

Similar Reads