Pergunta

started programming javascript a few months ago. I've been using the 'document.write()' command initially but having it wipe my html each time it's used is a little counterproductive.

Using the getElementByID("div's ID").innerHTML = or anything you think is better, what would effectively run this code but add it to whatever's currently in the div, not replace it.

x = x + 1
document.write( "you have clicked this button" + x + "times")

So like after 3 clicks it should say:

you have clicked this button 1 times
you have clicked this button 2 times
you have clicked this button 3 times

Thanks very much.

Foi útil?

Solução

element.insertAdjacentHTML('beforeend', htmlString);

Where element is a reference to a DOM element (e.g. returned from document.getElementById).

Demo

insertAdjacentHTML Reference


Note: though element.innerHTML += htmlString is also possible, it is usually a bad practice as all of the element's innerHTML would be re-parsed into new DOM elements, trashing out the old elements and their attached listeners/data.

Outras dicas

Did you try console.log(""). the console.log() is used to display your string on the console of your chrome or firefox browser. Right Click on the browser and select inspect element, then select console tab on topright. any errors in the page and any function called on console will be displayed here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top