Is there any different between
$(element).empty()
and $(element).text('');
Which one is better?
Is there any different between
$(element).empty()
and $(element).text('');
Which one is better?
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
.