JavaScript – Referencing CSS Properties Containing Hyphens or Dashes

by Michael Scepaniak on November 23, 2010 in software development

If, in JavaScript, you need to reference/manipulate CSS properties at runtime that contain a hyphen or dash in their names, omit the hyphen and, instead, camelcase the surrounding letters. You need to do this because hyphens in JavaScript variable names are illegal. For example, this won’t work:

div.style.background-color = "red"; div.style.z-index = 3;

Whereas this will:

div.style.backgroundColor = "red"; div.style.zIndex = 3;

Hope this helps.

Mike
Want to be notified when new articles are posted?
Enter your email address:

{ 0 comments… add one now }

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed

Previous post:

Next post:

Member of The Internet Defense League