0

I want to be able to append a replay button on to my canvas when the player loses my javascript game.

I have HTML:

<canvas id="myCanvas" >

</canvas>

And part of my javascript:

var canvas = document.getElementById("myCanvas");

...

if (gameover){
    var div = document.createElement('div');
    div.innerHTML = '<button onclick="window.reload">replay</button>';
    canvas.appendChild(div);
}

When I inspect my window the HTML has been added, but it doesn't show up on the screen?

S Craig
  • 291
  • 1
  • 4
  • 15

1 Answers1

2

The canvas element cannot render HTML, You have to add a new sibling to the canvas, so your HTML code renders after the canvas.

Maybe this would help you: How to do insert After() in JavaScript without using a library?

Community
  • 1
  • 1
Bardo
  • 2,470
  • 2
  • 24
  • 42