Sometimes, we want to remove an element instead of appending to it with jQuery.
In this article, we’ll look at how to remove an element instead of appending to it with jQuery.
Opposite of Append in jQuery
To remove an element instead of appending to it with jQuery, we can use the remove
method.
For instance, if we have the following HTML:
<div>
</div>
Then we can write:
const newUL = $('<ul><li>test</li></ul>');
$('div').append(newUL);
setTimeout(() => {
newUL.remove();
}, 3000)
to create the ul element with:
const newUL = $('<ul><li>test</li></ul>');
Then we can append it to the div with:
$('div').append(newUL);
And finally, we remove the ul after 3000 milliseconds by writing:
setTimeout(() => {
newUL.remove();
}, 3000)
Conclusion
To remove an element instead of appending to it with jQuery, we can use the remove
method.