wafflesausage
wafflesausage

Reputation: 395

Javascript Button Disappears

I'm having an issue with a basic Javascript program. When this script is run as-is, the button disappears. I'm trying make it so it continues to print the life total while either respawning the button or keeping it in place. Is there an easy way to do this? I have a snippet included below to demonstrate what I'm trying to do.

<html>
<input type="button" value = "+" onclick="addLife()">
<script>
var player1life = 20;
function addLife()
{
    player1life++;
    document.write(player1life);
    document.write("<br>");
}   

</script>
</html>

Upvotes: 2

Views: 320

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117334

You cannot use write() after the loading of the document, it will overwrite the complete document.

Use a DOM-method, e.g. appendChild() to inject nodes into the document.

->Example

Upvotes: 5

Related Questions