Articles

Most efficient way to empty an element with jQuery

In jQuery on October 9, 2011 by patrellery


There’s more than one way to empty an element with jQuery.

$(selector).html('');
$(selector).text('');

These are all valid but might not be as efficient as the empty() function.

$(selector).empty();

This is definitely the most clear way, and almost definitely the most efficient because by definition these call the empty() in their implementation. In addition, they invoke html parsers rather than just dealing with dom nodes like the other methods.

So go check your code now!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: