+ 3

jQuery empty() vs html("") vs children().remove()

What is the best? Any diference? Best performance, avoiding memory leaks and so on... Do all ways remove attached event handlers, registered data()... ?

8th Jan 2021, 10:55 AM
David OrdĂĄs
David OrdĂĄs - avatar
1 Answer
+ 2
The .empty() method will clear the contents of the selected element (let’s say a <div> element), along with the child nodes (if any). The .remove() method will remove the selected element (or the parent element), completely, from the web page. Although, you can save the contents of the removed element, you however cannot retrieve the events or data of the element that you have removed. However, html("") is nothing but displaying nothing.
9th Jan 2021, 10:07 AM
Arun Bhattacharya
Arun Bhattacharya - avatar