Javascript - Div Content Without Innerhtml
Solution 1:
DEMO:http://jsfiddle.net/Cb6ME/
// get the divvar div = document.getElementById('foo');
// remove child nodes while at least one existswhile( div.childNodes[0] ) {
div.removeChild( div.childNodes[0] );
}
// create a new span elementvar 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 );
Solution 2:
You can use nodeValue
to access the value of a node, however the value of a div
. In your example you might have the following HTML...
<div id="myLovelyDiv">This is the text that you want to change</div>
and this script...
var myDiv = getElementById("myLovelyDiv");
myDiv.childNodes[0].nodeValue = "The text has been changed.";
but I fail to see why you wouldn't use
myDiv.innerHTML = "The text has been changed properly.";
Solution 3:
A DIV element is a generic block level (by default) element in HTML used as a structural container to hold one or more block or inline elements.
Depending on what it is you want to change you can either select the sub-node in question directly, loop over the childNodes property to find the desired sub-node or completely rewrite the contents as html using innerHTML (which you stated you didn't want to do).
If you want to add content you can create a new element and use the appendChild(child) method of the DIV element to add to it's contents.
Is that what you were looking for?
Solution 4:
I know I'm late but .textContent
can be replaced for .innerHTML
(if you only want to change the text and not code HTML).
Post a Comment for "Javascript - Div Content Without Innerhtml"