By KdgDev

2009-04-05 00:09:19 8 Comments

I'm wondering if there's a way to change the text of anything in HTML without using innerHTML.

Reason I'm asking is because it's kinda frowned upon by the W3C. I know it's nitpicking, but I just wanna know, is there a way?

EDIT: people seem to misunderstand what I'm asking here: I want to find a way to effectivly change the text being displayed.

If I have:

<div id="one">One</a>

innerHTML allows me to do this:

var text = document.getElementsById("one");
text.innerHTML = "Two";

And the text on my screen will have changed.
I do not wish to append more text, I wish to change allready existing text.


@Heals Legodi 2019-02-08 19:55:58

insertAdjacentHTML() is the way to go. Read more: Click for documentation

@Jeroen Heier 2019-02-08 20:20:07

While this link may answer the question, it is better to include a usable code example here and provide the link for reference.

@user7674857 2017-03-07 21:18:59

Also looking for a good alternative to bypass element.innerHTML I finally found that solution:

HTMLElement.prototype.htmlContent = function(html)
    var dom = new DOMParser().parseFromString('<template>'+html+'</template>', 'text/html').head;

//-- document.getElementById('my-id').innerHTML = string_of_html;

Another alternative without <template> tags, but loop instead:

HTMLElement.prototype.htmlContent = function(html)
    var dom = new DOMParser().parseFromString(html, 'text/html').body;
    while (dom.hasChildNodes()) this.appendChild(dom.firstChild);

Keep in mind that this method actually 'add' content when innerHTML 'replace' content...

This may help:

HTMLElement.prototype.clearContent = function()
    while (this.hasChildNodes()) this.removeChild(this.lastChild);

//-- document.getElementById('my-id').innerHTML = '';


@Code83 2013-10-10 13:18:44

I sometimes find it helpful to store a direct reference to the text node if I am going to be updating it regularly. I do something like this:

var dynamicText = myDiv.appendChild(document.createTextNode("the initial text"));

And then whenever I need to update it, I just do this:

dynamicText.nodeValue = "the updated text";

This prevents having to walk the DOM or add or remove any children.

@Shamik 2011-09-24 07:54:02

Well i f i understand your question properly this should be an answer.

var text = document.getElementById("one");
//text.innerHTML = "Two";

@BlackMagic 2010-11-28 12:07:39


Replace text.innerHTML = 'two' with text.firstChild.nodeValue = 'two'.

@ilya n. 2009-06-16 10:02:48

It appears to me that the CSS+HTML+JS combination should achieve desired effects:

.myelement:before {   
    content: attr(alt);


<span class='myelement' alt='initial value'></span> 


element.setAttribute('alt', 'new value'); 

Does anyone know if this works in practice?

@Turnor 2009-04-05 00:20:32

The recommended way is through DOM manipulation, but it can be quite verbose. For example:

// <p>Hello, <b>World</b>!</p>
var para = document.createElement('p');
para.appendChild(document.createTextNode('Hello, '));

// <b>
var b = document.createElement('b');


// Do something with the para element, add it to the document, etc.


In response to your edit, in order to replace the current content, you simply remove the existing content, then use the code above to fill in new content. For example:

var someDiv = document.getElementById('someID');
var children = someDiv.childNodes;
for(var i = 0; i < children.length; i++)

But as someone else said, I'd recommend using something like jQuery instead, as not all browsers fully support DOM, and those that do have quirks which are dealt with internally by JavaScript libraries. For example, jQuery looks something like this:

$('#someID').html("<p>Hello, <b>World</b>!</p>");

@KdgDev 2009-04-06 19:44:09

so actually, first place new text and then remove old... that seems a very lenghty way to do things... I wonder why the W3C does not approve of innerHTML

@Turnor 2009-04-06 20:21:15

Other way around :) Their view is that with innerHTML, it's possible to insert invalid markup into an XML/XHTML document. Though that said, they seem to have changed their minds with HTML5.

@Marco Demaio 2010-10-14 12:13:08

jQuery uses innerHTML many times inside code, therfor suggesting to use jQuery in order avoid using innerHTML does not make much sense IMHO.

@Alejandro García Iglesias 2012-08-08 14:20:10

Also, you should properly check your "not all browsers fully support DOM" statement in the context of this question. It's true that some browsers (mainly IE) have poor support of DOM, but, for this subject, methods are fully supported. Check compatibility tables:

@Frederik Voordeckers 2018-01-23 15:21:56

A nicer way to remove all the childnodes would be while (el.firstChild) { el.removeChild(el.firstChild); } Looks cleaner and no need for an extra indexed loop

@kennebec 2009-04-06 14:08:04

var who=document.getElementById('one'), txt='new text';
if(who.innerText) who.innerText=txt;
else if(who.textContent) who.textContent= txt;

This may be as objectionable as innerHTML to you, but it has the advantage of working in some cases (IE) where innerHTML or appendChild do not, like some table nodes, the text of style and script elements and the value of form fields

@IonuČ› G. Stan 2009-04-05 17:25:43

If you only want to change plain text, then there's a quicker solution that relies on standards:

document.getElementById("one") = "two";

Anyway, please note that innerHTML is going to be part of the upcoming HTML 5 standard.

@Bill the Lizard 2009-04-05 03:30:00

You can get the same effect by manipulating the DOM. The safest way to change text is to remove all the child nodes of the element and replace them with a new text node.

var node = document.getElementById("one");

while( node.firstChild )
    node.removeChild( node.firstChild );
node.appendChild( document.createTextNode("Two") );

Removing the child nodes gets rid of the text content of your element before replacing it with the new text.

The reason most developers avoid using innerHTML is that accessing elements through the DOM is standards compliant.

@Sergej Andrejev 2009-04-05 00:23:32

You could use DOM as follows:

<script type="text/javascript">
var element = document.getElementsByTagName("div")[0];

But I think anyone rarely do this but use a framework like jQuery or Prototype or any other javascript framework out there instead. This is jquery example:

<script type="text/javascript" src="//"></script>
<script type="text/javascript">
var element = $("div");

@Andrew Hare 2009-04-05 00:17:49

The better way of doing it is to use document.createTextNode. One of the main reasons for using this function instead of innerHTML is that all HTML character escaping will be taken care of for you whereas you would have to escape your string yourself if you were simply setting innerHTML.

@Steve Harrison 2009-04-05 00:58:10

However, it seems that innerHTML is much faster than the W3C DOM methods:

@bobince 2009-04-05 01:53:35

It depends what you're doing; it's misleading to say one method is blanket better. DOM tends to slow when doing childNode list operations on lots of elements (tending to O(n²)), where innerHTML can do them all at once. But innerHTML's parsing/serialisation step can make other operations slower.

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] How to replace innerHTML of a div using jQuery?

20 Answered Questions

[SOLVED] Can scripts be inserted with innerHTML?

4 Answered Questions

[SOLVED] Altering innerHTML is unwantedly affecting div dimensions

5 Answered Questions

[SOLVED] Angular 2 - innerHTML styling

  • 2016-03-28 15:04:23
  • Jakob Svenningsson
  • 128415 View
  • 171 Score
  • 5 Answer
  • Tags:   angular innerhtml

14 Answered Questions

[SOLVED] ES6 class variable alternatives

2 Answered Questions

[SOLVED] How to create a clickable span between the text in a div with Javascript

1 Answered Questions

[SOLVED] Adding a ASP Control to page from Code Behind within InnerHTML

1 Answered Questions

Add innerhtml to top links

5 Answered Questions

[SOLVED] <pre> tag loses line breaks when setting innerHTML in IE

4 Answered Questions

[SOLVED] document.getElementById().innerHTML fails with 'Unknown Error' in IE

  • 2009-07-08 07:48:59
  • ZA.
  • 6680 View
  • 0 Score
  • 4 Answer
  • Tags:   javascript

Sponsored Content