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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With