canvas html | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

canvas html

why canvas element must have id ?

5th Jun 2017, 5:54 PM
MUHAMAD NOORRIZAL BIN MINHAT
MUHAMAD NOORRIZAL BIN MINHAT - avatar
2 Answers
+ 20
Not necessarily...
5th Jun 2017, 6:26 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
Because that's the best way to refer to the element in your script. If you want to draw anything on the canvas, you need to do it with JavaScript. It doesn't absolutely need an ID, you could still find it by other means (DOM tree traversal), but it's the simplest and clearest method. When you've given it the ID "theCanvas", for example, you can start editing it thusly: var canvas = document.getElementById("theCanvas");
5th Jun 2017, 10:33 PM
Taija
Taija - avatar