jquery append() and prepend()
The jQuery append() and prepend() methods are used to manipulate the content of selected elements by inserting new elements or text within them.
jQuery append() method
The append() method adds content at the end of the selected elements. It can accept various types of input, such as HTML strings, DOM elements, or jQuery objects. This method is often used to add new elements or text as the last child of the selected elements.
The above code will append an item to the list, meaning that the new item will be inserted as the last item.
- Item 1
- Item 2
- Item 3
jQuery prepend()
The prepend() method inserts content at the beginning of the selected elements. Like append(), it can also take different types of input. This method is useful when you want to insert content as the first child within the selected elements.
- Itel 1
- Item 2
- Item 3
The above code will prepend an item to the list, meaning that the new item will be inserted at the beginning of the list.
Full SourceConclusion
Both methods are commonly used for dynamically populating HTML structures, creating new elements on-the-fly, and enhancing user interfaces by adding or inserting content without requiring a page refresh.