jQuery Tutorial: Navigating and Manipulating DOM Elements

What is DOM (Document Object Model)?
Web browsers manage HTML documents as a tree structure, not just plain text.
For example, consider the following HTML:
<div id="wrap">
<h1>Title</h1>
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Grape</li>
</ul>
</div>
Browsers understand this as a structure with parent-child relationships.
div
├── h1
└── ul
├── li
├── li
└── li
This structure is called the DOM (Document Object Model).
jQuery provides various functions to freely navigate and modify the DOM.
What You'll Learn in This Lesson
In this session, we'll practice these concepts:
Finding parent elements
Finding child elements
Finding sibling elements
Adding new elements
Removing elements
Cloning elements
Replacing elements
These features are used in almost all websites, including forums, e-commerce sites, and admin panels.
Preparation for Practice
First, write the following HTML:
<div id="box">
<h2>Fruit List</h2>
<ul id="fruit">
<li>Apple</li>
<li>Banana</li>
<li>Grape</li>
</ul>
</div>
parent()
Selects the parent element of the current element.
$("#fruit").parent().css("border","2px solid red");
After execution, a red border will be added to the div, which is the parent of ul.
This is one of the most commonly used functions when selecting parent elements.
parents()
Selects all ancestor elements.
$("#fruit").parents();
This finds all parent elements up to the body tag.
If you want to find a specific parent, you can use:
$("#fruit").parents("div");
children()
Selects only direct child elements.
$("#fruit").children();
Or:
$("#fruit").children("li");
This selects all li elements.
find()
Finds all descendant elements within the current element.
$("#box").find("li");
This finds all li elements inside the current element.
In practice, this is one of the most frequently used DOM navigation functions.
next()
Selects the next sibling element.
$("li:first").next().css("color","red");
The element following the first li is selected, which changes "Banana" to red.
prev()
Selects the previous sibling element.
$("li:last").prev().css("color","blue");
The element immediately before the last item is selected.
siblings()
Selects all sibling elements except for itself.
$("li:first").siblings();
All li elements except the first one are selected.
This is often used in menu activation features.
first() and last()
Selects the first element
$("li").first();
Selects the last element
$("li").last();
These are frequently used when selecting the first or last item in a list.
eq()
Selects an element at a specific position.
$("li").eq(1);
Indexing starts from 0:
0 = Apple
1 = Banana
2 = Grape
append()
Adds new content at the end of an element.
$("#fruit").append("<li>Strawberry</li>");
The result will be:
Apple
Banana
Grape
Strawberry
This is a common method used in forum comment features.
prepend()
Adds content at the beginning.
$("#fruit").prepend("<li>Watermelon</li>");
The result will be:
Watermelon
Apple
Banana
Grape
before()
Adds content before the current element.
$("#fruit").before("<h3>Fruits</h3>");
A heading will be added above the ul element.
after()
Adds content after the current element.
$("#fruit").after("<p>End</p>");
A paragraph will be added below the ul element.
remove()
Completely removes an element.
$("#fruit li:last").remove();
The last li will be removed.
Removed elements cannot be restored.
empty()
Removes only the content, not the element itself.
$("#fruit").empty();
The ul remains, but all li elements inside it are removed.
clone()
Copies an element.
$("#fruit").clone().appendTo("#box");
The list is copied and added below the original.
This is often used in practice when copying templates.
replaceWith()
Replaces the current element with another element.
$("h2").replaceWith("<h1>New Title</h1>");
The h2 is replaced with an h1.
appendTo()
This is the opposite of append().
$("<li>Peach</li>").appendTo("#fruit");
It achieves the same result as append().
Practical Example 1: Adding Comments
$("#addBtn").click(function(){
$("#commentList").append(
"<li>New comment.</li>"
);
});
This is the most commonly used feature in forums.
Practical Example 2: Creating a Delete Button
$(".delete").click(function(){
$(this).parent().remove();
});
This deletes only the row containing the current button.
Practical Example 3: Menu Activation
$("li").click(function(){
$(this)
.addClass("active")
.siblings()
.removeClass("active");
});
This activates only the currently selected menu item.
This code is very commonly used in practice.
Practical Example 4: Adding Products
$("#add").click(function(){
$("#product").append(
"<li>New product</li>"
);
});
This is a common method used in e-commerce admin panels.
Common Mistakes for Beginners
Mixing up remove() and empty()
remove()
The element itself is removed.
empty()
The element remains, but its contents are removed.
Mixing up append() and prepend()
append()
Adds to the end
prepend()
Adds to the beginning
Difference between find() and children()
children()
Selects only direct children
find()
Selects all descendant elements
Practice Assignment
Assignment 1
When the button is clicked, add "Orange" to the end of the fruit list.
Assignment 2
When the button is clicked, add "Honeydew" at the beginning of the list.
Assignment 3
Create a button to delete the last fruit in the list.
Assignment 4
Copy the entire list and add it below the original list.
Assignment 5
Create a menu where clicking an item activates only that item with the 'active' class.
Key Takeaways
DOM refers to the structure of HTML elements.
parent(), parents() find parent elements.
children(), find() find child elements.
next(), prev(), siblings() find sibling elements.
append(), prepend() add new elements.
before(), after() add content before or after the current element.
remove() removes an element completely.
empty() removes only the content, not the element itself.
clone() copies elements.
replaceWith() replaces one element with another.
Conclusion
In this session, we learned various methods for navigating the DOM and adding or removing HTML elements.
These features are essential in almost all websites, including forums, e-commerce sites, admin panels, and comment systems.
Especially find(), append(), remove(), siblings(), clone() functions are frequently used in practice, so it's beneficial to become familiar with them through hands-on examples.
In the next lesson, we'll learn about handling loops, arrays, and objects using jQuery's each() function for efficient data processing.
FAQ
What is the DOM?
The Document Object Model (DOM) represents HTML documents as a tree structure of nodes, allowing JavaScript to interact with and manipulate web page elements.
How do I add an element to the end of a list using jQuery?
Use the append() method. For example: $("#list").append("<li>New Item</li>");
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Jquery 강좌] 6강 DOM 탐색과 요소 추가·삭제하기