jQuery - Add Elements


Using jQuery makes it simple to include fresh elements or content in your webpage.


Add New HTML Content

We're going to explore four jQuery functions that help us insert fresh content into a web page:

  • 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 adds stuff to the BOTTOM of the chosen parts of a webpage.


jQuery prepend() Method

The jQuery method prepend() adds content right at the beginning of the selected HTML elements.


Add Several New Elements With append() and prepend()

In the two examples provided earlier, we've simply added text or HTML content at the start or end of the chosen HTML elements.

In the case of the append() and prepend() methods, you can add as many new elements as you want. These new elements can be made using text/HTML, jQuery, or JavaScript code and DOM elements, like our earlier examples.

In this example, we make some new things. We create these things using text/HTML, jQuery, and JavaScript/DOM. After that, we add these new things to the text using the append() method (we could have used prepend() as well).


jQuery after() and before() Methods

The jQuery after() function adds stuff right after the chosen parts of a webpage.

The jQuery before() function adds stuff in front of the chosen parts of a webpage.


Add Several New Elements With after() and before()

Additionally, the after() and before() methods can accept a limitless amount of new elements as inputs. New elements can be made using text/HTML, jQuery, JavaScript code, and DOM elements, like the example shown before.

In this example, we make different new elements using text/HTML, jQuery, and JavaScript/DOM. After that, we add these new elements to the text using the after() method (you could also use before() for the same result):