2

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.

3
  • you might want to look into jquery animate function api.jquery.com/animate (there is sample code in the middle of the page) Commented May 16, 2012 at 19:27
  • What does "wanted to have it move over a period of 1 second" mean? You want it to slide over? You can use CSS3 transforms for that or jQuery Animate to do it easily. Commented May 16, 2012 at 19:29
  • I also suggest jQuery, all you'd have to do is: $('#purple').animate({'margin-right':'34px'},1000) Commented May 16, 2012 at 19:36

2 Answers 2

5

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..

Sign up to request clarification or add additional context in comments.

Comments

0

Seems like you missed the quotes on the function getElementById.

Like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.