Jump to content

Having a problem with variables.


Recommended Posts

Ok i have this code:

<script type="text/javascript">function move(){document.getElementById('object').style.position="absolute";document.getElementById('object').style.left="400";var xpos = document.getElementById('object').style.left;var CMD = document.getElementById('object').value;if(CMD = 'right'){document.getElementById('object').style.left=xpos - 10;}}</script>

I whas trying to move the object if the value of the textbox whas right.But i get a error saying invalid argument , it is most likely because xpos = 400px .But how do i solve that?

Link to comment
Share on other sites

var xpos = document.getElementById('object').style.left;...document.getElementById('object').style.left=xpos - 10;

Yeah, I think the problem is because you are trying to subtract 10 from "400px". Try using offsetLeft rather than style.left to get the position.
var xpos = document.getElementById('object').offsetLeft;...document.getElementById('object').style.left = (xpos - 10) + "px";

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...