Difference between empty() and remove() in jquery.

The  methods empty(), remove()  are used for removing elements from DOM but they are different.

empty() :

This method removes all the child element of the matched element where remove() method removes set of matched elements from DOM.

remove():

Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

consider the following example.

<body>
           <div id="div1">
                       <p> this is an example snippet</p>
           </div>

</body>

if we ues $("#div1").empty() it will remove <p >tag and see the code below.

<body>
           <div id="div1">
                      
           </div>

</body>

if we use $("#div1").remove() it will remove whole <div> tag.

<body>
           

</body>

Posted on by