I've created the following...
var menu = document.createElement('select');
How would I now set CSS attributes e.g width: 100px
?
I've created the following...
var menu = document.createElement('select');
How would I now set CSS attributes e.g width: 100px
?
That's actually quite simple with vanilla JavaScript:
Use
element.style
:All of the answers tell you correctly how to do what you asked but I would advise using JavaScript to set a class on the element and style it by using CSS. That way you are keeping the correct separation between behaviour and style.
Imagine if you got a designer in to re-style the site... they should be able to work purely in CSS without having to work with your JavaScript.
In prototype I would do:
Just for people who want to doing the same thing in 2018
You can assign a CSS custom property to your element (through CSS or JS) and change it:
Assigment through CSS:
Assignment through JS
Get property value through JS
Here useful links: