0

Hi i want to know if there is a way of setting a style of on object through javascript using a string, i don't know how to explain it very well hopefully my code will

function updateElementFromValue(inputval, valueOfStyle)
{
  selectedActiveElement.style.valueOfStyle = inputval.value + "px";
}

so i have this function and when i call it, inputval will be an input field and valueOfStyle will be passed as top or left or width etc so it effectively becomes, ignore selectedActiveElement as it is a global object.

  selectedActiveElement.style.top = 200 + "px";

i figure im not calling it right but i think their is a way of doing this.

Thanks for you help

1
  • 4
    selectedActiveElement.style[valueOfStyle] = inputval.value + "px"; ? Commented Jan 15, 2015 at 12:59

1 Answer 1

2

You can use the setProperty function
element.style.setProperty(valueOfStyle, inputval.value + "px")

Also you're able to do sth. like this:
element.style[valueOfStyle] = inputval.value + "px"
but I would not recommend it since not every browser supports that.

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

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.