2

I want to replace a span element with the id "randMsg" with the string "saying". Here is what I have right now:

 document.getElementById('randMsg').write(saying);

Any ideas? I’m a JavaScript noob, what am I doing wrong?

4
  • 1
    .write() is the wrong functions to use. try document.getElementById('...').innerHTML = ... Commented Jun 2, 2013 at 2:55
  • I am assuming you want to set the text of the span. Try this document.getElementById('randMsg').innerHTML("saying"); Commented Jun 2, 2013 at 2:56
  • Duplicate? innerHTML without the html, just text (I think that question is concerned with getting the value, rather than setting it, however.) Commented Jun 2, 2013 at 3:07
  • @PSL You're confusing DOM with jQuery. Did you mean $('#randMsg').innerHTML("saying"); or some such? Commented Jun 2, 2013 at 10:27

3 Answers 3

2

You can use the textContent property to update the text inside the element:

document.getElementById("randMsg").textContent = "Replaced Content";

http://jsfiddle.net/RaGng/

Or if you need it to work in IE8 and below, you can detect support for textContent, and if it is not supported, you can use the non-standard innerText instead:

var el = document.getElementById("randMsg"),
    msg = "Replaced Content";

("textContent" in el) ? el.textContent = msg : el.innerText = msg;

http://jsfiddle.net/RaGng/4/

Sign up to request clarification or add additional context in comments.

3 Comments

Have a look at the support for textContent developer.mozilla.org/en-US/docs/Web/API/Node.textContent
@PSL There are modern browsers, current browsers aaaaannnnd there's IE bringing up the rear as always.
Fair enough, but then you can use "in" to detect support, and then use innerText if not supported: jsfiddle.net/RaGng/3
1

The following W3C DOM code works in all mainstream browsers, including IE8 and older.

var node = document.getElementById('randMsg');
var textToUse = 'Hello, World!';

// Remove all the children of the node.
while (node.hasChildNodes()) {
    node.removeChild(node.firstChild);
}

// Now add the text.
node.appendChild(document.createTextNode(textToUse));

Working JsFiddle here.

You can also use innerText, however, not supported in Firefox:

node.innerText = textToUse;

Or, you can use textContent, however, not supported by IE versions 8 and older:

node.textContent = textToUse;

Quirksmode has very well maintained browser compatibility tables for all of the above.

Comments

0

Working jsFiddle Demo

You must set the innerHTML property of the element. Consider the following markup:

<span id="randMsg"></span>

And in your JS code:

var saying = 'Say Hello World';
document.getElementById('randMsg').innerHTML = saying;

And your result would be:

<span id="randMsg">Say Hello World</span>

Note

Don't forget to add this script after your element (or wait for DOM ready):

<body>
    <span id="randMsg"></span>
    <script>
        var saying = 'Say Hello World';
        document.getElementById('randMsg').innerHTML = saying;
    </script>
</body>

1 Comment

innerHTML may not work properly if the text includes characters like < or &. Sounds like you want innerText, which will accept the string as text, but which, of course, lacks Firefox support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.