2

So I saw this question - Get/Set CSS property values via Javascript: questions and this solution - http://www.w3schools.com/jsref/dom_obj_style.asp.

Where my question differs is I am building a WYSIWYG editor and I want to dynamically set the properties via native javascript. So I was wondering if there was a way I could do something like this:

document.getElementById("myH1").style.property("color") = "red";
4
  • 3
    Yes. Bracket notation: document.getElementById("myH1").style["color"] = "red"; Commented Aug 12, 2015 at 15:22
  • @blex that works for something like color but your will have to still dromedaryCase ones like background-image, no? If not, thats cool - I didn't know it supported both syntaxes. Commented Aug 12, 2015 at 15:23
  • @somethinghere: Yes, that's right, backgroundImage. Commented Aug 12, 2015 at 15:24
  • Also must point out, and this is certainly extra-topical, but this is WAY easier using jQuery. The very thing it was designed for, in fact. Commented Aug 12, 2015 at 16:18

2 Answers 2

4

The "proper way" is using setProperty or setPropertyValue:

element.style.setProperty("background-color", "red");
element.style.setPropertyValue("background-color", "red");

They behave the same, the only difference is that setProperty accepts an optional third argument to set !important priority.

However, for convenience, the CSSStyleDeclaration interface is extended by partial interfaces in order to allow to get or set the values of supported CSS properties using IDL camel-case attributes.

That means you can also use

element.style.backgroundColor = "red";
element.style["backgroundColor"] = "red";
Sign up to request clarification or add additional context in comments.

Comments

2

Yeah, you can use the style name in camel case notation, or target it like you would an array name:

document.getElementById("myH1").style.color = "red";
document.getElementById("myH1").style["color"] = "red";

Here's a fiddle with it in action: http://jsfiddle.net/m7kb2Lsa/

If you want to see all of the styles you can set on an element with the correct camel casing the easiest way I've found is to type something like this in the console (assuming the element exists on the page), this will give you a full list of everything you can set:

document.getElementById("myH1").style

4 Comments

Wouldn't it be very useful if you expanded your answer and mentioned that properties with multiple words need to be dromedaryCased? Otherwise he's in for another SO question that could've easily be resolved right here, right now.
@somethinghere but doesn't the SO question mentioned in this question here address all that? Sure, Matthew could copy and paste a lot of stuff, but I think it would all be redundant.
@MrLister I'm not seeing a SO question referenced in this answer... I just believe that expanding the answer a little bit with some information on the pitfalls of a solution will help the OP a lot in the future, and anyone who reads this. He did mention CamelCasing in his edit, and how to find the right keys, so I find this a +1
@somethinghere he said in the OP's QUESTION, not the answer. Other than that...well I've got no dog in this kennel.

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.