dezman
dezman

Reputation: 19358

Move image with javascript

What is wrong with this function?

function moveColor()
 {
 document.getElementById(purple).style.marginRight = "34px";
 }

with this html:

<div><img src="images/purple.png" id="purple" onclick="colorpurple()" onmouseover="moveColor()" style="cursor:pointer;"/></div>

I also wanted to have it move over a period of 1 second, but can't seem to solve this simple problem.

Upvotes: 2

Views: 317

Answers (2)

Bruno Costa
Bruno Costa

Reputation: 2720

Seems like you missed the quotes on the function getElementById.

Like this:

function moveColor() { 
      document.getElementById('purple').style.marginRight = "34px";
}

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195962

You need to put the id in quotes (so that it is treated as a string).

document.getElementById('purple').style.marginRight = "34px";

The current usage means that purple refers to a variable, which is not defined so it has an undefined value and so the document.getElementById method returns nothing..

Upvotes: 5

Related Questions