Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery empty() vs text('')

Is there any different between

$(element).empty() and $(element).text('');

Which one is better?

like image 776
Atif Avatar asked Apr 04 '13 12:04

Atif


1 Answers

Result is the same, but if we look at jQuery text method implementation:

text: function (value) {
    return jQuery.access(this, function (value) {
        return value === undefined
            ? jQuery.text(this)
            : this.empty().append((this[0] && this[0].ownerDocument || document).createTextNode(value));
    }, null, value, arguments.length);
},

we will see that $(element).text(''); will actually use .empty internally. Hence latter is a little faster.

Also from semantic point of view if you want to empty some node you should definetely use .empty method, not text.

like image 191
dfsq Avatar answered Sep 19 '22 13:09

dfsq