jQuery Inserts Elements

jQuery inserts elements dynamically inside the existing HTML document. These jQuery methods are: append(), prepend(), after(), before().

jQuery append() Method

jQuery append() is used to insert content at the end of the selected elements.

general Syntax

    
     $("p").append("Step by step tutorial.");
   

jQuery prepend() Method

It is used to insert content at the beginning of the selected elements.

    
        $("p").prepend("Learn Bootstrap 5");
   

Insert Multiple Elements with append() & prepend() Method

jQuery append() and prepend() method also takes multiple argument as a input.

Let us understand it with the help of an example

Source Code

          
            $(document).ready(function(){
  $(".btn-multi-insert").click(function(){
    var mainHeading = "<h2>Learn Jquery</h2>";
    var pargarphText = document.createElement("p");
    pargarphText.innerHTML = "<h3>Follow step by step process.</h3>";  
    $("body").append(mainHeading,pargarphText);
  
  });
});          
        
Try it now

jQuery before() Method

jQuery before() Method is very helpful to insert content before the selected HTML elements.

Source Code

          
            $(document).ready(function () {
  $(".btn-multi-insert").click(function () {
    $(".center-element").before("<h1>Learn Jquery</h1>");
  });
});          
        
Try it now

jQuery after() Method

jQuery after() method is useful for inserting content after the selected HTML elements.

Source Code

          
            $(document).ready(function () {
  $(".btn-multi-insert").click(function () {
    $(".center-element").after("<h3>To make interactive application.</h3>");
  });
});          
        
Try it now

Insert Multiple Elements with before() & after() Method

These methods also take multiple arguments as input. Let us understand it with the help of an example.

Source Code

          
            $(document).ready(function () {
  $(".btn-multi-insert").click(function () {
    var Headingtext = "<h2>Learn jQuery</h2>";
    var nextHeadingText = "<h2>step by step</h2>";
    var paragraphText = document.createElement("p");
    paragraphText.innerHTML = "<b>t o make an interactive application.<b>";
    $("p").before(Headingtext, nextHeadingText, paragraphText);
  });
});          
        
Try it now

Web Tutorials

jQuery Inserts Elements
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4