How to append String to a Div in JavaScript
Use innerHTML
. If you want to change the div content, use =
or if you want to append something to the content that already exists, use +=
.
<script>
document.getElementById('yourElementId').innerHTML += 'Your Value';
// or
document.getElementById('yourElementId').innerHTML = 'Your Value';
</script>
Example 1:
Change the div content
<div id="result">Waiting for result</div>
<script>
document.getElementById('result').innerHTML = 'Hello';
</script>
Output: Hello
Example 2:
Append text to the div without changing its content.
<div id="result">Result: </div>
<script>
document.getElementById('result').innerHTML += 'Hello';
</script>
Output: Result: Hello