Name

addRule( ) — NN n/a IE 4 DOM n/a

Synopsis

addRule(selector, style, [index])

Adds a new rule for a style sheet. This method offers a scripted way of adding a rule to an existing styleSheet object:

document.styleSheets[1].addRule("P B","color:red")

You may duplicate a selector that already exists in the styleSheet and, therefore, override an existing rule for the same element selector. The only prohibition is that you may not override a rule to convert a plain style rule into one that creates a positionable element. The new rule is governed by the same cascading rules as all style sheet rules (that includes the rule’s source code position among other rules with the same selector). Therefore, a new rule in a styleSheet object does not supersede a style set in an element’s STYLE property.

Returned Value

None.

Parameters

selector

The style rule selector as a string.

style

One or more style attribute:value pairs. Multiple pairs are semicolon delimited, just as they are in the regular style sheet definition.

index

An optional integer indicating where in the collection the new element should be placed.

Get Dynamic HTML: The Definitive Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.