Skip to content
the poor coder

jquery empty

Poor Man
Published
Reading time
2 min read
On this page

What is jQuery empty() method?#

jQuery empty() method is used to remove all the child elements and text content from the selected element(s). The method does not remove the selected element(s) itself, only its content.

Syntax:#

Text
$(selector).empty();

Example:#

Consider the following HTML code:

Text
<div id="myDiv">
    <p>This is a paragraph.</p>
    <ul>
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
    </ul>
</div>

If we call empty() method on the div element, all its child elements and text content will be removed, resulting in an empty div element:

Text
$("#myDiv").empty();

The resulting HTML code will look like this:

Text
<div id="myDiv"></div>

Multiple Ways to Use empty() Method:#

  • We can pass a selector to empty() method to remove only the child elements that match the selector.
  • We can also pass a function to empty() method that returns the content to be removed. The function receives two arguments: index and html. Index is the index of the element being removed and html is the content of the element.

Using Selector:#

Consider the following HTML code:

Text
<div id="myDiv">
    <p>This is a paragraph.</p>
    <ul>
        <li class="item">List item 1</li>
        <li>List item 2</li>
        <li class="item">List item 3</li>
    </ul>
</div>

If we want to remove only the list items with class "item", we can pass the selector to empty() method:

Text
$("#myDiv").find(".item").empty();

The resulting HTML code will look like this:

Text
<div id="myDiv">
    <p>This is a paragraph.</p>
    <ul>
        <li></li>
        <li>List item 2</li>
        <li></li>
    </ul>
</div>

Using Function:#

Consider the following HTML code:

Text
<div id="myDiv">
    <p>This is a paragraph.</p>
    <ul>
        <li class="item">List item 1</li>
        <li>List item 2</li>
        <li class="item">List item 3</li>
    </ul>
</div>

If we want to remove only the list items whose content starts with "List item", we can pass a function to empty() method:

Text
$("#myDiv").find("li").empty(function(index, html) {
    if (html.startsWith("List item")) {
        return html;
    }
});

The resulting HTML code will look like this:

Text
<div id="myDiv">
    <p>This is a paragraph.</p>
    <ul>
        <li class="item"></li>
        <li></li>
        <li class="item"></li>
    </ul>
</div>

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC