I've created the following...
var menu = document.createElement('select');
How would I now set CSS attributes e.g width: 100px
?
Just as you can use JavaScript to change the HTML in a web page, you can also use it to change CSS styles. The process is very similar. After you've selected an element, you can change its style by attaching the style property to the selector, followed by the style you want to change.
You can change CSS using the jQuery css() method which is used for the purpose of getting or setting style properties of an element. Using this method you can apply multiple styles to an HTML all at once by manipulating CSS style properties.
The setProperty() method sets a new or modifies an existing CSS property in a CSS declaration block.
Use element.style
:
var element = document.createElement('select'); element.style.width = "100px";
Just set the style
:
var menu = document.createElement("select"); menu.style.width = "100px";
Or if you like, you can use jQuery:
$(menu).css("width", "100px");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With