html – How do I change the text of a span element using JavaScript?

The Question :

413 people think this question is useful

If I have a span, say:

<span id="myspan"> hereismytext </span>

How do I use JavaScript to change “hereismytext” to “newtext”?

The Question Comments :

The Answer 1

678 people think this answer is useful

For modern browsers you should use:

document.getElementById("myspan").textContent="newtext";

While older browsers may not know textContent, it is not recommended to use innerHTML as it introduces an XSS vulnerability when the new text is user input (see other answers below for a more detailed discussion):

//POSSIBLY INSECURE IF NEWTEXT BECOMES A VARIABLE!!
document.getElementById("myspan").innerHTML="newtext";

The Answer 2

78 people think this answer is useful

Using innerHTML is SO NOT RECOMMENDED. Instead, you should create a textNode. This way, you are “binding” your text and you are not, at least in this case, vulnerable to an XSS attack.

document.getElementById("myspan").innerHTML = "sometext"; //INSECURE!!

The right way:

span = document.getElementById("myspan");
txt = document.createTextNode("your cool text");
span.appendChild(txt);

For more information about this vulnerability: Cross Site Scripting (XSS) – OWASP

Edited nov 4th 2017:

Modified third line of code according to @mumush suggestion: “use appendChild(); instead”.
Btw, according to @Jimbo Jonny I think everything should be treated as user input by applying Security by layers principle. That way you won’t encounter any surprises.

The Answer 3

29 people think this answer is useful
document.getElementById('myspan').innerHTML = 'newtext';

The Answer 4

28 people think this answer is useful

EDIT: This was written in 2014. You probably don’t care about IE8 anymore and can forget about using innerText. Just use textContent and be done with it, hooray.

If you are the one supplying the text and no part of the text is supplied by the user (or some other source that you don’t control), then setting innerHTML might be acceptable:

// * Fine for hardcoded text strings like this one or strings you otherwise 
//   control.
// * Not OK for user-supplied input or strings you don't control unless
//   you know what you are doing and have sanitized the string first.
document.getElementById('myspan').innerHTML = 'newtext';

However, as others note, if you are not the source for any part of the text string, using innerHTML can subject you to content injection attacks like XSS if you’re not careful to properly sanitize the text first.

If you are using input from the user, here is one way to do it securely while also maintaining cross-browser compatibility:

var span = document.getElementById('myspan');
span.innerText = span.textContent = 'newtext';

Firefox doesn’t support innerText and IE8 doesn’t support textContent so you need to use both if you want to maintain cross-browser compatibility.

And if you want to avoid reflows (caused by innerText) where possible:

var span = document.getElementById('myspan');
if ('textContent' in span) {
    span.textContent = 'newtext';
} else {
    span.innerText = 'newtext';
}

The Answer 5

19 people think this answer is useful

I use Jquery and none of the above helped, I don’t know why but this worked:

 $("#span_id").text("new_value");

The Answer 6

7 people think this answer is useful

Here’s another way:

var myspan = document.getElementById('myspan');

if (myspan.innerText) {
    myspan.innerText = "newtext";
}
else
if (myspan.textContent) {
        myspan.textContent = "newtext";   
}

The innerText property will be detected by Safari, Google Chrome and MSIE. For Firefox, the standard way of doing things was to use textContent but since version 45 it too has an innerText property, as someone kindly apprised me recently. This solution tests to see if a browser supports either of these properties and if so, assigns the “newtext”.

Live demo: here

The Answer 7

5 people think this answer is useful

In addition to the pure javascript answers above, You can use jQuery text method as following:

$('#myspan').text('newtext');

If you need to extend the answer to get/change html content of a span or div elements, you can do this:

$('#mydiv').html('<strong>new text</strong>');

References:

.text(): http://api.jquery.com/text/

.html(): http://api.jquery.com/html/

The Answer 8

0 people think this answer is useful

Like in other answer, innerHTML and innerText are not recommended, it’s better use textContent. This attribute is well supported, you can check it this: http://caniuse.com/#search=textContent

The Answer 9

0 people think this answer is useful
document.getElementById("myspan").textContent="newtext";

this will select dom-node with id myspan and change it text content to new text

The Answer 10

0 people think this answer is useful

You may also use the querySelector() method, assuming the ‘myspan’ id is unique as the method returns the first element with the specified selector:

document.querySelector('#myspan').textContent = 'newtext';

developer.mozilla

The Answer 11

0 people think this answer is useful

You can do

 document.querySelector("[Span]").textContent = "content_to_display"; 

The Answer 12

0 people think this answer is useful

(function ($) {
    $(document).ready(function(){
    $("#myspan").text("This is span");
  });
}(jQuery));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="myspan"> hereismytext </span>

user text() to change span text.

The Answer 13

0 people think this answer is useful

I used this one document.querySelector('ElementClass').innerText = 'newtext';

Appears to work with span, texts within classes/buttons

The Answer 14

-1 people think this answer is useful

For this span

<span id="name">sdfsdf</span>

You can go like this :-

$("name").firstChild.nodeValue = "Hello" + "World";

Add a Comment