Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set CSS property in JavaScript?

Tags:

javascript

css

I've created the following...

var menu = document.createElement('select'); 

How would I now set CSS attributes e.g width: 100px?

like image 236
Skizit Avatar asked Mar 04 '11 14:03

Skizit


People also ask

Can we change CSS property using JavaScript?

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.

Can we change CSS property using JavaScript or jQuery?

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.

What is set property in JavaScript?

The setProperty() method sets a new or modifies an existing CSS property in a CSS declaration block.


2 Answers

Use element.style:

var element = document.createElement('select'); element.style.width = "100px"; 
like image 148
KJYe.Name Avatar answered Sep 20 '22 15:09

KJYe.Name


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"); 
like image 37
djdd87 Avatar answered Sep 20 '22 15:09

djdd87