9

I want to add multiple lines of CSS with JavaScript. I know I can do this:

document.getElementById(id).style.property=new style

as in:

<!DOCTYPE html>
<html>
<body>

<h1 id="id1">My Heading 1</h1>

<button type="button" 
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button>

</body>
</html>

But, the above code allows me add just a single CSS Property. If a want to add more than one property, like this:

#id1 {
    color: red;
    background-color: beige;
    padding-bottom: 2px;
    margin: 3px;
}

How do I add all of this by not repeating:

document.getElementById(id).style.property=new style

....again and again. Thanks in advance !

2

7 Answers 7

21

document.getElementById("id1").setAttribute(
  "style", "color: green; background-color: beige; padding-bottom: 2px; margin: 3px;");
<body>
  <h1 id="id1">My Heading 1</h1>
  <button type="button" onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button>

</body>

Here is a fiddle.

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

Comments

6

Yes you can add multiple CSS with JavaScript like this

your button

<input id="myButton" type="button" value="Hello">

And the JavaScript for it

document.getElementById('myButton')
  .style.cssText = "color: blue; width: 100px; height: 55px; border: 1px solid red;";

Comments

4

Why not add a class that has all of the properties defined already?

document.getElementById(id).classList.add('new_class')

Comments

3

If your styles are dynamically generated and you can't simply add a new CSS class to this element, you could do something like this, using the same idea that you posted but coding in a DRY manner.

This is also useful if your element already has some styles applied and you don't want to overwrite them.

Click run code snippet below to see the demo.

var id  = "content";

var styles = { 
  'color': 'red',
  'background-color': 'beige',
  'padding-bottom': '2px',
  'margin': '3px'
};

var elementStyle = document.getElementById(id).style;
for(var styleName in styles) {
  elementStyle[styleName] = styles[styleName];
}
<div id="content">
  This is a test
</div>

Comments

1
document.getElementById('the_object_ID')
  .setAttribute( 'style', 'color:white; margin:20px;');

1 Comment

You may want to add a little context around the answer. Also, what if style is already set on the object, and the code should extend that style with additional properties?
0

you can create a function for all this

<button type="button" onclick="myFunc('id1')"> Click Me! </button>

in js

function myFunc(id){
    var myDiv = document.getElementById(id);
    myDiv.style.color = 'red';
    myDiv.style.backgroundColor = "beige"; 
    myDiv.style.paddingBottom = "2px";
    myDiv.style.margin = "3px";
}

Comments

0

Simplest answer is using .style like this :

document.getElementById("id1").style = "height:30px; width:30px; background-color:red;"

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.