0


Ok, I understand JavaScript addRule with simple css rules:

firstRule.style.color = "red";

But how do I add for example this:

-webkit-transform:translateX(16em) perspective(600px) rotateY(10deg)";

Something like this?:

firstRule.style.-webkit-transform = "translateX(16em) perspective(600px) rotateY(10deg)";

Because it does not work. Any ideas?

3 Answers 3

1

camelize it (replace any dash with uppercase character that comes after the dash):

firstRule.style.WebkitTransform = "translateX(16em) perspective(600px) rotateY(10deg)";

You can also use .setProperty:

firstRule.style.setProperty( "-webkit-transform", "translateX(16em) perspective(600px) rotateY(10deg)" );
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it this way:

firstRule.style["-webkit-transform"] = firstRule.style["-moz-transform"] = "translateX(16em) perspective(600px) rotateY(10deg)";

Just an example to adding the '-moz' prefix as well.

Comments

0
document.getElementById('firstRule').setAttribute('style','xxxxxx');

clean and simple

3 Comments

This overwrites all other styles, it's not so simple when you need to retain them by concating strings
just get the actual style and set it with document.getElementById('firstRule').setAttribute('style', variable+'xxxxxx'); not that hard if you think a little
I never said string concating is hard :P just annoying

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.