JavaScript can set any CSS commands and is "almost" using the name of the CSS command as the JavaScript property. However, there is a problem: Some characters, like the dash, are not allowed within a JavaScript property. But many CSS commands like, for instance, font-weight, do have dashes in them. Therefore, the JavaScript language uses a lowerCamelCase syntax: Every component starts with an uppercase letter, but not the very first one. So the CSS command font-weight can be set using the fontWeight property. All CSS commands may be accessed using the style property of every styleable HTML element on the page. There are two common ways to access these elements:
The following listing shows the latter approach. The <p> element is selected using document.getElementById(); then the font-weight CSS command is set: Changing a CSS Command (style.html)
Tip When a Mozilla browser is used, the JavaScript console also shows an error when an invalid value for the chosen style will be applied, as Figure 4.1 shows. This is extremely convenient when you're debugging. Figure 4.1. Mozilla browsers (here: Firefox) complain about invalid CSS values. |