Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - div content without innerHTML

Tags:

javascript

dom

I wanted to ask how to change div content, but not using innerhtml.

like image 883
Kedor Avatar asked Jun 20 '26 10:06

Kedor


1 Answers

DEMO: http://jsfiddle.net/Cb6ME/

   // get the div
var div = document.getElementById('foo');

   // remove child nodes while at least one exists
while( div.childNodes[0] ) {
    div.removeChild( div.childNodes[0] );
}
   // create a new span element
var span = document.createElement( 'span' );

   // give it some text content
span.appendChild( document.createTextNode("I'm new!!!") );

   // append the span to the original div
div.appendChild( span );
like image 52
user113716 Avatar answered Jun 22 '26 23:06

user113716



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!