jQuery Remove Text, Element [remove(), empty(), unwrap()]

jQuery Remove Text, Element [remove(), empty(), unwrap()]

In this tutorial, you will learn how to remove text and element from html using jQuery empty(), remove(), unwrap() method by id, name class and tag.

jQuery Remove Text and Element Methods

There are three types of method available to remove the text, element or content, you can use these methods to remove the text, element or content.

  • empty() method
  • remove() method
  • unwrap() method

jQuery empty() Method

The empty() method in jQuery is used to remove all child elements (including text and other nodes) from the selected HTML element(s). It essentially clears the content of the element, leaving it empty. This method is commonly used when you want to remove the content of an element or a group of elements, but not the element itself.

Here’s the basic syntax of the empty() method:

$(selector).empty();

Example of jQuery empty() method

You can see that example of the empty() method, the following example will remove all the contents inside the elements with the class on the click of the button.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Removing all the Contents using empty() jQuery</title>
<style type="text/css">
.container{
    padding: 15px;
    background: #1EBBA3;
    border: 1px solid #bead18;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_empty").click(function(){
       $("#contents").empty();
    });
});
</script>
</head>
<body>
    <div id="contents" class="container">
        <h1>Hello World!</h1>
        <p class="hint"><strong>Note:</strong> If you click the following button it will remove all the contents of the container div including the button.</p>
        <button type="button" id="btn_empty">Click Here</button>
    </div>
</body>
</html>                            

Output

Removing all the Contents using empty() jQuery

Hello World!

Note: If you click the following button it will remove all the contents of the container div including the button.

jQuery remove Method

The jQuery remove() method can remove the selected html elements from the DOM as well as everything inside it. This method also removes the data and events of the selected elements.

Syntax

$(selector).remove(selector);

Parameters of remove() method

ParameterDescription
Selectoris an optional parameter. It specifies whether to remove one or more elements. If you have to remove more than one element then you should separate them with comma (,).

Example of jQuery remove() method

You can see that example of the remove() method, the following example will remove the selected paragraph.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Removing all the Contents using empty() jQuery</title>
<style type="text/css">
.container{
    padding: 15px;
    background: #1EBBA3;
    border: 1px solid #bead18;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_remove").click(function(){
       $("#con_div").remove();
    });
});
</script>
</head>
<body>
    <div class="container">
        <h1>Hello World!</h1>
        <p id="con_div"><strong>Note:</strong> If you click the following button it will remove this paragraph</p>
        <button type="button" id="btn_remove">Click Here</button>
    </div>
</body>
</html>                            

Output

Removing all the Contents using empty() jQuery

Hello World!

Note: If you click the following button it will remove this paragraph

jQuery unwrap()

Using the jQuery unwrap() method can remove the parent element of the selected elements.

Syntax

$(selector).unwrap(); 

Example of jQuery unwrap() method

You can see that the unwrap() method, the following example will remove the original element of
elements on the button click.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Removing all the Contents using empty() jQuery</title>
<style type="text/css">
.container{
    padding: 15px;
    background: #1EBBA3;
    border: 1px solid #bead18;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $("#btn_wrap").click(function(){
       $("#con_wrap").unwrap();
    });
});
</script>
</head>
<body>
    <div class="container">
        <h1>Hello World!</h1>
        <p id="con_wrap"><strong>Note:</strong> If you click the following button it will remove the parent element of this paragraph.</p>
        <button type="button" id="btn_wrap">Click Here</button>
    </div>
</body>
</html>                            

Output

Removing all the Contents using empty() jQuery

Hello World!

Note: If you click the following button it will remove the parent element of this paragraph.

Recommended jQuery Tutorial

  1. jQuery Remove Attribute and Disabled Attribute From Element
  2. jQuery | Event MouseUp By Example
  3. Event jQuery Mouseleave By Example
  4. jQuery Event Mouseenter Example
  5. Event jQuery MouseOver & MouseOut By Example
  6. keyup jquery event example
  7. Jquery Click Event Method with E.g.
  8. Event jQuery. Blur By Example
  9. jQuery form submit event with example
  10. keydown function jQuery
  11. List of jQuery Events Handling Methods with examples
  12. Jquery Selector by .class | name | #id | Elements
  13. How to Get the Current Page URL in jQuery
  14. jQuery Ajax Get() Method Example
  15. get radio button checked value jquery by id, name, class
  16. jQuery Set & Get innerWidth & innerHeight Of Html Elements
  17. jQuery Get Data Text, Id, Attribute Value By Example
  18. Set data attribute value jquery
  19. select multiple class in jquery
  20. How to Remove Attribute Of Html Elements In jQuery
  21. How to Checked Unchecked Checkbox Using jQuery
  22. jQuery removeClass & addClass On Button Click By E.g.
  23. To Remove whitespace From String using jQuery
  24. jQuery Ajax Post Method Example
  25. jQuery Ajax Get Method Example
  26. To Load/Render html Page in div Using jQuery Ajax $.load
  27. jQuery Sibling Methods – To Find Siblings Elements
  28. jQuery Find Siblings Elements with Class, id

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *