You are here: Reference > JavaScript > client-side > style handling > properties > parentRule (rule objects)

parentRule property (rule objects)

Browser support:
9
Returns a reference to the CSSRule object that contains the current CSSRule object.
Note: The parentRule property is supported in Internet Explorer from version 9.
This property can be used for rules in a @media at-rule. In that case, it retrieves a CSSRule object that represents the @media style rule. This property is rarely needed, because style rules are only accessible through their parent rules or style sheets.

Syntax:

object.parentRule;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Reference to the parent rule.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the parentRule property:
<head>
    <style id="myStyle">
        @media all {
            H1 {
                color:#00aacc;
            }
        }
    </style>

    <script type="text/javascript">
        function GetMediaRule () {
            var styleTag = document.getElementById ("myStyle");

                // the style sheet in the style tag
            var sheet = styleTag.sheet ? styleTag.sheet : styleTag.styleSheet;

            if (sheet.cssRules) {   // all browsers, except IE before version 9
                // the @media rule
                var mediaRule = sheet.cssRules[0];

                // the H1 rule
                var h1Rule = mediaRule.cssRules[0];

                if (h1Rule.parentRule) {
                        // the @media rule, again
                    mediaRule = h1Rule.parentRule;
                    alert ("The media rule:\n" + mediaRule.cssText);
                }
                else {  // Safari
                    alert ("Your browser does not support the parentRule property!");
                }
            }
            else {  // Internet Explorer before version 9
                alert ("Your browser does not support this example!");
            }
        }
    </script> 
</head>
<body>
    <button onclick="GetMediaRule ()">Get the media rule with parentRule!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

User Contributed Comments

Post Content

Post Content