I am trying to use Javascript in order to add a canvas to one page which originally does not have one. I am trying to do the following:
var canv=document.createElement("canvas");
canv.setAttribute("id", "canvasID");
alert(canv.id);
var c=document.getElementById("canvasID");
alert(c.id);
The problem is the the first alert(canv.id) results in canvasID, while the second alert is undefined because c is null.
Can anybody tell me what am I doing wrong?
PS: the code is designed to run under Greasemonkey so adding the canvas and its ID in the HTML itself is not a viable option.
Use something like
Node.appendChild( child )
for adding it to the DOM:Or you can use
element.innerHTML
:I use this all the time and works great...
Without something like that third line, your new canvas is never actually inserted into the page.