Take the 2-minute tour ×

Append text in already appended div id "txt". i have use appendChild() function,added "Testing This is" But i need "This is Testing"

document.getElementById("txt").appendChild("This is");




This is Testing

Help me

share|improve this question
add comment

3 Answers

you can do it like this:

var old_string = document.getElementById("div_id").innerHTML;
var new_string = "is Testing";
document.getElementById("div_id").innerHTML = new_string + " " + old_string;

Hope that helps.

share|improve this answer
add comment
$("#txt").before("before text");
$("#txt").after("after text");


or you can befit from the chaining

$("#txt").before("before ").after("after ");
share|improve this answer
As same div "txt" load image append before. How to append image –  jey Feb 9 '12 at 10:17
add comment

You identified the answer in the tags you put on the question - take a look at the .prepend() jQuery function. Rather than adding child nodes to the element at the end, it adds them at the beginning. Assuming that #txt is an element that originally contains only the text node 'Testing', then this code:

$('#txt').prepend('This is ');

will give you the results you want.

Working DEMO

share|improve this answer
add comment

Your Answer


