3

I just stumbled upon a js project where I need to set the value inside a html label tag using javascript. I tried the follwing code but, for some reason its not working. any idea why?

<label id="text"></label>
<script>
    document.getElementById('text').value = 'Hello World!';
</script>

any help would be greatly appreciated.

Dexygen
  • 12,287
  • 13
  • 80
  • 147

3 Answers3

6

You can use document.getElementById('text').textContent instead.

document.getElementById('text').textContent = 'Hello \nlcr World!';;
<label id="text"></label>

There is other options, such as .innerHtml or .innerText, to see how .textContent differs from the others and what the benefits in using one over the other might be, see this ► https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

Nope
  • 22,147
  • 7
  • 47
  • 72
1
document.getElementById('text').innerHTML = 'Hello World!';
user3117628
  • 776
  • 1
  • 15
  • 35
0
document.getElementById('text').innerText = 'Hello World!';
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103