Techniques for Inserting Content into HTML Documents with jQuery
To learn how to insert content into HTML documents using jQuery, readers can refer to the following jQuery lesson by Mytour.
1. Inserting Content into HTML Documents Using jQuery
jQuery provides a variety of methods, including append(), prepend(), html(), text(), before(), after(), wrap(), ... enabling users to insert new content inside existing elements.
2. The append() Method in jQuery
The append() method in jQuery is used to insert content at the end of selected elements.
Example: The example below illustrates the use of the append() method in jQuery to insert some HTML content into text paragraphs in the document:
The output result looks like this:
Note: Contents or elements inserted using the append() and prepend() methods in jQuery will be added inside the selected elements.
3. Using the prepend() Method in jQuery
The prepend() method in jQuery is used to insert content at the beginning of selected elements.
Example: The example below illustrates how to insert HTML content into text paragraphs in the document using the prepend() method:
The resulting output looks like this:
4. Inserting Multiple Elements Using the append() and prepend() Methods
Moreover, the append() and prepend() methods in jQuery also accept multiple arguments as input.
Example: The following code snippet illustrates how to insert the h1, p, and img elements within the body element:
The resulting output appears as follows:
5. Using the before() Method in jQuery
The before() method in jQuery is utilized to insert content before the selected elements.
Example: The following example illustrates inserting a piece of text before the target element in the existing document using the before() method in jQuery:
The resulting output appears as follows:
Note: Content or elements inserted using the before() and after() methods in jQuery are added outside the selected elements.
6. Using the after() Method in jQuery
The after() method in jQuery is used to insert content after the selected elements.
Example: The following example demonstrates how to insert a piece of text after the target element in the document using the after() method in jQuery:
The output result looks like this:
7. Inserting Multiple Elements Using the before() and after() Methods in jQuery
Similarly, the before() and after() methods in jQuery also allow multiple arguments as input.
Example: The example below illustrates how to insert the h1, p, and img elements before the p elements:
The resulting output appears as follows:
8. The wrap() Method in jQuery
The wrap() method in jQuery is used to wrap HTML structure around selected elements.
Example: The jQuery code below demonstrates wrapping container elements with the div element using the .wrapper class in the document, wrapping content inside paragraphs starting with the p and em elements:
The resulting output looks like this:
In this HTML lesson, Mytour has just guided you on how to insert content into HTML documents using jQuery. Additionally, if you have any questions or need clarification, readers can leave their comments below the article. In the next lesson, Mytour will introduce you to how to remove elements and attributes in jQuery.
