Pergunta

I usually use jQuery but am starting to develop in pure javascript. I'm trying to create a variable for an element that I can use throughout the code but I can't seem to get it working. At the moment I'm trying:

var canvas = document.getElementById('canvas').value;

Then using that variable like:

canvas.style.backgroundPosition = '0 0';

Can someone please tell me what I'm doing wrong that is cause the variable to not be read or not declared properly?

Foi útil?

Solução

element.value returns the value as a string, not the element (and a canvas has no value).
For just the element remove the .value part

var canvas = document.getElementById('canvas');

canvas.style.backgroundPosition = '0 0';
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top