What is the functional difference between these three jQuery
methods:
remove() – Removes all child elements with selected element. In this method you can restore all data but not event handlers of the removed elements from the DOM. All data and events related with elements will be removed. detach() – Removes all child elements with selected elements.
jQuery detach() Method The detach() method removes the selected elements, including all text and child nodes. However, it keeps data and events. This method also keeps a copy of the removed elements, which allows them to be reinserted at a later time.
5) Which of the following jQuery method is used to hide the selected elements? Explanation: The jQuery hide() method is used to hide the selected elements.
hide()
sets the matched elements' CSS display
property to none
.
remove()
removes the matched elements from the DOM completely.
detach()
is like remove()
, but keeps the stored data and events associated with the matched elements.
To re-insert a detached element into the DOM, simply insert the returned jQuery
set from detach()
:
var span = $('span').detach(); ... span.appendTo('body');
Imagine a piece of paper on a table with some notes written with pencil.
hide
-> throw a clothe onto itempty
-> remove the notes with an eraserdetach
-> grab the paper in your hand and keep it there for whatever future plansremove
-> grab the paper and throw it to the dustbinThe table represents the current DOM space, the paper represents the element, and the notes represent the contents (child nodes) of the element.
A bit simplified and not completely accurate, but easy to understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With