2.5.2.3 addRule

addRule()

Used to create a new rule in a style sheet. Up to 4095 rules can be added to a single style sheet with this method. If you apply rules to a disabled style sheet, they do not apply until the style sheet is enabled.

Parameters

sSelectorof type DOMString

String that specifies the selector for the new rule. Only single selectors are valid; grouped selectors cause "Invalid Argument" error.

sDeclarationof type DOMString

String that specifies one or more semi-colon separated declarations.

iIndexof type long

Optional. Ordinal index that specifies the requested position of the object in the collection. If this value is not supplied, or if the value is larger than the number of items in the collection, or if value is -1, the rule is added to the end of the collection.

Return Value

-1         Reserved. Always returns -1.

JScript Error

E_INVALIDARG (0x80040057)  Raised for grouped selectors, or more than 4095 style rules.

Example

The following example demonstrates how to add a rule to the style sheet.

 <style type="text/css">
 p {
    color:red;
 }
 </style>
 <script type="text/javascript">
 window.onload = function() {
    var s = document.styleSheets[0];
    var idx = s.addRule('#test:hover','color:green');
 }
 </script>
 </head>
  
 <body>
 <p id="test">This text should turn green on hover.</p>
 </body>