Code has been added to clipboard!

Removing Child Elements With jQuery .empty()

Reading time 2 min
Published Jan 18, 2018
Updated Oct 2, 2019

jQuery empty: Main Tips

  • The jQuery .empty() method removes all descendant nodes from selected DOM elements.
  • All strings of text are also considered child elements and thus removed.
  • To remove elements completely, use the .remove() method. To remove them but keep their data and event handlers, use .detach().

Usage of jQuery .empty() Explained

The .empty() jQuery method removes the children and nodes of the specified elements. All you need to do is specify a selector to match the target elements:

$(selector).empty();

Check the example with the selector defined as <p> below:

Example
$(document).ready(() => {
	$("button").click(() => {
	    $("p").empty();
	});
});

Note: if you make jQuery remove all children elements, nested elements will be deleted as well.

Differences Between jQuery .empty(), .detach() and .remove()

There are three methods used for removing elements in jQuery: .empty(), .detach() and .remove(). A beginner might find it a bit confusing. You will find the differences of these methods explained in the table below:

Method Definition
.empty() Removes the content and child elements from the selected element, but does not remove the element itself.
.detach() Removes all child elements with the selected element, but keeps data and event handlers, so you may re-add them at a later time.
.remove() Removes all child elements, data and events with the selected element.

Compare the methods in the example below:

Example
$(document).ready(function () {

  $("button.btn-remove").click(function () {
    $("#textbox1").remove();
  });

  $("button.btn-empty").click(function () {
    $("#textbox2").empty();
  });

  $("button.btn-detach").click(function () {
    $("#textbox3").detach();
  });
Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX