How to add to an object using innerHTML

Is there a way to add information to a DOM object with .innerHTML without replacing what exists there?

For instance: document.getElementById('div').innerHTML = 'stuff';

Would return <div id="div">stuff</div>

And then a similar call: document.getElementById('div').innerHTML = ' and things';

Would set the div to <div id="div">stuff and things</div>

+1
source share
7 answers
document.getElementById('mydiv').innerHTML = 'stuff'
document.getElementById('mydiv').innerHTML += 'and things'
document.getElementById('mydiv').innerHTML += 'and even more'
+11
source
+4
source

Element.insertAdjacentHTML().

insertAdjacentHTML() HTML XML DOM . , , , , . , innerHTML.

If you want to add yours stuffat the end of this Element, you would do

const div = document.getElementById("div");
div.insertAdjacentHTML('beforeend', 'stuff');;
+4
source

why not try

document.getElementById('div').innerHTML += ' and things';

pay attention to the "+"

+3
source

There are errors in your examples. But you could just do this:

var myDiv = document.getElementById('div');

myDiv.innerHTML = myDiv.innerHTML + ' and things';
+1
source

change it to this:

document.getElementById.innerHTML('div') = 
    document.getElementById.innerHTML('div') + ' and things';

concatenation style ol 'x = x + 1.

although I don't think document.getElementById.innerHTML ('div') is even the correct syntax? Don't you mean "document.getElementById (" division "). InnerHTML"?

0
source

May be:

document.getElementById('div').innerHTML = document.getElementById('div').innerHTML + ' and things'
-1
source

All Articles