Using jQuery, what's the performance difference between using:
$('#somDiv').empty().append('text To Insert')
and
$('#somDiv').html('text To Insert')
?
Using jQuery, what's the performance difference between using:
$('#somDiv').empty().append('text To Insert')
and
$('#somDiv').html('text To Insert')
?
The correct syntax is
difference between
append()
andhtml()
in jQuery.append()
and.html()
are the most useful methods in jQuery. But these are far different from one another,.append()
add some value to the existing one. Where.html()
do the same but it removes the old value first.Here is an example:
Now I will use
.append()
to add one<li>
, For that I will write:The output of this jQuery will be:
Now if I use
.html()
to add one<li>
, For that I will write:The output of this Script will be:
Here in this example
.append()
add one extra<li>
, whether.html()
removes the old one with new one. This is the main difference between.append()
and.html()
in jQuery..html will overwrite the contents of the DIV.
.append will add to the contents of the DIV.
In simple words:
works like this:
becomes:
And innerHTML replaces the contents, so it becomes this:
Understand the meaning and functions of the extensions provided in jQuery.
-- Above code will clear the div id tag content first then will append the text to the target div:
-- Above code replace the div tag text with the html text:
$('#somDiv').html(value)
is equivalent to$('#somDiv').empty().append(value)
.Source: jQuery source.