Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
8
rated 0 times [  11] [ 3]  / answers: 1 / hits: 7201  / 10 Years ago, fri, april 11, 2014, 12:00:00

What is the best way to create new text node and append it to the DOM?



I'm currently using this:



$('#someDiv').append(document.createTextNode('Some text'));


but is it the best way? Are there any other alternatives?



UPDATE



after checking the jQuery code I found that my variant probably the best...



here is how .text() function implemented in jQuery:



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 );
},


so, .text() function calls .createTextNode() method, so if you wish to save on calling extra operations, it's better to use direct text's node .append()


More From » jquery

 Answers
2

Like this :



var yourtext = 'Some text';
$('#someDiv').append(yourtext);

[#46100] Thursday, April 10, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
khalilb

Total Points: 173
Total Questions: 110
Total Answers: 105

Location: Honduras
Member since Thu, Mar 23, 2023
1 Year ago
;