0

I want to change a css property of a HTML element using Javascript. I can't find a solution to my problem. I would like to use the element with the id of CreateMenu, and apply the variable called value to the property specified by the type variable, but it doesn't seem to support variables.

edit: Type is the variable i want to use to get which property i want to edit.

function style(name, type, value) {
    document.getElementById("CreateMenu").style.type = value;
}
1
  • 2
    there is no style property called .type ... there may be one [type] though Commented Aug 19, 2021 at 10:26

2 Answers 2

1

I did not test the following but you can access an object property by its key name this way:

function style(name, type, value) {
    document.getElementById("CreateMenu").style[type] = value;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Mark my answer as correct, if it worked ;)
1

Using object "dot" notation

document.getElementById("CreateMenu").style.type = value;

you're assigning to, literally .style.type

What you need to do in this case is use bracket notation

so

document.getElementById("CreateMenu").style[type] = value;

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.