How to Remove Contents of an Element (div, ul, etc) with jQuery

Written by Richi González Friday, 21 December 2012

Hello,

As I’ve been recently working with a website that has a lot of jQuery and AJAX, and my jQuery knowledge was small, I was wondering how to remove the content of a <div> with jQuery. The solution was pretty easy and with just a line of code.

The element we are going to affect will be called ‘Target’.

1. Set the id of the Target to ‘ourtargetid’ (for example).

<div id='ourtargetid'></div>

2. In the <head> section of your document, wrapped between JavaScript tags, you will use the function remove().

$(element).remove();

So for your div we will use:

$('#ourtargetid').remove();

Easy as that. Now if you would like to remove something by its class attribute, we would use:

$('.classname').remove();

And that’s it. It is really simple, yet very useful when you want to remove an element dynamically. If you have any question, please do not hesitate to contact me.

Best Regards,

Richi

Owner of Juapo2Services

Looking for quality Web Hosting? Look no further than Arvixe Web Hosting!


Leave a Reply






+ 3 = six