Removal

These methods allow us to delete elements from the DOM.

detach

Detach the set of matched elements from the DOM while preserving all the data and events.

Detach single element

jQuery

element.detach();

Vanilla JS

element.remove();

Detach the set of elements

jQuery

elements.detach();

Vanilla ES6+

elements.forEach(x => x.remove());

Vanilla ES5

elements.forEach(function(element) {
    element.remove();
});

remove

Remove the set of matched elements from the DOM while not preserving any associated data or events.

Remove single element

jQuery

element.remove();

Vanilla JS

// No equivalent

Remove the set of elements

jQuery

elements.remove();

Vanilla JS

// No equivalent

empty

Remove all child nodes of the set of matched elements from the DOM.

Empty single element

jQuery

element.empty();

Vanilla JS

element.innerHTML = '';

Empty the set of elements

jQuery

elements.empty();

Vanilla ES6+

elements.forEach(x => x.innerHTML = '');

Vanilla ES5

elements.forEach(function(element) {
    element.innerHTML = '';
});
shopify traffic stats