Total Page Preview:   000000000121

What is jQuery empty() and detach() ?

jQuery empty() and detach()

In jQuery, both empty() and detach() are methods used to manipulate the content of elements, but they have different purposes.

1-empty() Method:
The empty() method is used to remove all child nodes and content from the selected elements. It essentially clears the content within the specified elements.
In this example, clicking the "Empty Content" button will remove the <p> and <span> elements and their content from the #myDiv element.
 
2-detach() Method:
The detach() method is used to remove the selected elements from the DOM (Document Object Model). It is similar to remove(), but it retains the data and events associated with the removed elements.
In this example, clicking the "Detach Content" button will remove the entire #myDiv element and its content from the DOM. If you were to reinsert the detached elements back into the DOM, they would retain their previous state, including any attached event handlers.
 
 
 
 
 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments