About Lesson
Adding, Removing, and Modifying Elements
1. Adding Elements
- .append(): Adds content at the end of the selected element.
- .prepend(): Adds content at the beginning of the selected element.
- .after(): Adds content after the selected element.
- .before(): Adds content before the selected element.
JavaScript
$("#list").append("<li>Appended Item</li>");
$("#list").prepend("<li>Prepended Item</li>");
$("#list").after("<p>Content added after the list</p>");
$("#list").before("<p>Content added before the list</p>");
2. Removing Elements
- .remove(): Removes the element along with its children.
- .empty(): Clears the content of the element but keeps the element itself.
JavaScript
$("#item1").remove(); // Removes a specific item
$("#list").empty(); // Clears the list but keeps the <ul>
3. Cloning Elements
- .clone(): Creates a copy of the selected element.
JavaScript
const clonedItem = $("#item1").clone();
$("#list").append(clonedItem);