tags:

views:

353

answers:

6

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

For example: 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>

+3  A: 

The += operator.

David Dorward
A: 

change it to this:

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

the ol' x = x+1 style of concatenation.

though I don't think "document.getElementById.innerHTML('div')" is even proper syntax? Don't you mean "document.getElementById('divid').innerHTML"?

Brian Schroth
A: 

Maybe:

document.getElementById('div').innerHTML = document.getElementById('div').innerHTML + ' and things'
Jenea
+1  A: 

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

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

myDiv.innerHTML = myDiv.innerHTML + ' and things';
Jesper
+1 for identifying that there were mistakes in the OP source.
scunliffe
+1  A: 
document.getElementById('mydiv').innerHTML = 'stuff'
document.getElementById('mydiv').innerHTML += 'and things'
document.getElementById('mydiv').innerHTML += 'and even more'
duckyflip
+1  A: 

why not try

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

note the "+"

msparer