benhowdle89
benhowdle89

Reputation: 37454

Use Javascript variable in element's style property

function move(direction, el) {
    el.style.direction = (el.style.direction+10)+'px';
}

I have this very simple function that moves an element by 10 pixels in a specified direction. Now it errors if i use this, I'm thinking I can't use direction as a variable in this situation. Is this right?

Upvotes: 0

Views: 345

Answers (1)

John Kalberer
John Kalberer

Reputation: 5790

You need to access the style property like this:

function move(direction, el) {
    el.style[direction] = (el.style.direction+10)+'px';
}

As long as direction is a string.

Upvotes: 1

Related Questions