Loading Please Wait...

Logo Lynxsia IT Solutions

jQuery DOM Add

JQ DOM Add

With jQuery, it is easy to add new elements/content.

Add New HTML Content

We will look at four jQuery methods that are used to add new content:

  • append() - Inserts content at the end of the selected elements
  • prepend() - Inserts content at the beginning of the selected elements
  • after() - Inserts content after the selected elements
  • before() - Inserts content before the selected elements
jQuery append() Method

The jQuery append() method inserts content AT THE END of the selected HTML elements.

					 
        
          $("p").append("Some appended text.");
        
      
jQuery prepend() Method

The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML elements.

					 
        
          $("p").prepend("Some prepended text.");
        
      
Add Several New Elements With append() and prepend()

In both examples above, we have only inserted some text/HTML at the beginning/end of the selected HTML elements.

However, both the append() and prepend() methods can take an infinite number of new elements as parameters. The new elements can be generated with text/HTML (like we have done in the examples above), with jQuery, or with JavaScript code and DOM elements.

					 
        
          function appendText() {
            var txt1 = "<p>Text.</p>";              // Create element with HTML 
            var txt2 = $("<p></p>").text("Text.");  // Create with jQuery
            var txt3 = document.createElement("p"); // Create with DOM
            txt3.innerHTML = "Text.";
            $("body").append(txt1, txt2, txt3);     // Append the new elements
          }
        
      
jQuery after() and before() Methods

The jQuery after() method inserts content AFTER the selected HTML elements.

The jQuery before() method inserts content BEFORE the selected HTML elements.

					 
        
          $("img").after("Some text after");
          $("img").before("Some text before");
        
      
Add Several New Elements With after() and before()

Also, both the after() and before() methods can take an infinite number of new elements as parameters. The new elements can be generated with text/HTML (like we have done in the example above), with jQuery, or with JavaScript code and DOM elements.

					 
        
          function afterText() {
            var txt1 = "<b>I </b>";                 // Create element with HTML 
            var txt2 = $("<i></i>").text("love ");  // Create with jQuery
            var txt3 = document.createElement("b"); // Create with DOM
            txt3.innerHTML = "jQuery!";
            $("img").after(txt1, txt2, txt3);       // Insert new elements after 
          }
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved