Crystal
Crystal

Reputation: 29448

Change the absolute position of a div tag with Javascript

I have a div tag declared like this:

<div id="carRight" style="position:absolute; left:600px; top:170px;">
<img src="carRight.png" width="256" height="256" />
</div>

After a quick animation, I'd like to move the left position when a button is pressed. I tried a few variations of this, but it doesn't seem to be correct:

document.getElementById('carRight').style.left=600;
document.getElementById('carRight').style.position.left=600;

What is the correct statement to use? Thanks.

Upvotes: 8

Views: 70109

Answers (3)

shreyas
shreyas

Reputation: 1

It is not essential to write element.style.left="600px";. Just add document.getElementById('carRight').position='relative';.

Upvotes: 0

NJLaPrell
NJLaPrell

Reputation: 357

Try:

document.getElementById('carRight').style.left="600px";

Upvotes: 22

bensiu
bensiu

Reputation: 25564

document.getElementById('carRight').style="position:absolute; left:100px; top:170px;";

where 100px is new left position... position:absolute; part could be moved to class...

Upvotes: 3

Related Questions