jQuery Add


jQuery has various methods for inserting new content into an existing element, such as append(), prepend(), html(), text(), before(), after(), wrap(), and so on.


jQuery append() Method

To put material to the end of the chosen elements, use the jQuery append() function. On document ready, the following example will add some HTML to all the paragraphs, however on button click, it will append some text to the container element.

Example

Preview

jQuery prepend() Method

The prepend() function inserts material at the start of the specified components. On document ready, the following example will prepend some HTML to all the paragraphs, however on button click, it will prepend some text to the container element.

Example

Preview

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

The jQuery add() and prepend() functions also accept multiple parameters as input. The jQuery code in the following example will add a ,<h1>, <p>, and <image> element as the final three child nodes inside the <body> element.

Example

Preview

jQuery before() Method

To put material before the chosen components, use the jQuery before() technique. On document ready, the following example will put a paragraph before the container element, and on button click, it will place an image before the <h1> element.

Example

Preview

jQuery after() Method

The after() function of jQuery is used to inject material after the chosen components. On document ready, the following example will insert a paragraph after the container element, and on button click, it will insert an image after the <h1> element.

Example

Preview

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

The jQuery before() and after() functions also accept multiple parameters as input. The example below will place a <h1>, <p>, and an <image> element before the <p> elements.

Example

Preview

jQuery wrap() Method

The jQuery wrap() function is used to encircle the chosen items in an HTML structure. On document ready, the jQuery code in the following example will cover the container elements with a <div> element with the class. wrapper, while wrapping all the inner content of the paragraph elements first with the <b> and then with the <em> element.

Example

Preview