Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set content of HTML <span> with Javascript

In a webpage I am calling a WebService that gives me an integer value. I need to display this value in a block of text. I am currently using an HTML <span>.

So far, I've found two methods of putting my value in a span. innerText() is an IE proprietary way of doing it and innerHTML() is a non-standards compliant way, although widely supported.

What is the correct standards compliant way of setting the text between <span> and </span> from Javascript?

like image 978
Brian Beckett Avatar asked Jan 24 '11 16:01

Brian Beckett


People also ask

Can you use span in JavaScript?

You can use the HTML span tag as a container to group inline elements together so you can style or manipulate them with JavaScript.

Can we use innerHTML in span?

Bookmark this question.

How do you text the span of an element?

Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants.


1 Answers

With modern browsers, you can set the textContent property, see Node.textContent:

var span = document.getElementById("myspan"); span.textContent = "some text"; 
like image 191
robinst Avatar answered Oct 15 '22 01:10

robinst