You are here: Reference > JavaScript > client-side > style handling > properties > selectorText (CSSPageRule, CSSStyleRule, rule)

selectorText property (CSSPageRule, CSSStyleRule, rule)

Browser support:
Sets or retrieves a string that identifies the selector part of the current rule.

Syntax:

object.selectorText;
You can find the related objects in the Supported by objects section below.
This property is read/write.

Possible values:

String that specifies or retrieves to which elements a style rule applies.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the selectorText property:
<head>
    <style id="myStyle">
        .red {
            color: red;
        }
    </style> 

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

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

                // the first rule in the style sheet
            var rules = sheet.cssRules ? sheet.cssRules : sheet.rules;
            var firstRule = rules[0];

            alert (firstRule.selectorText);
        }
    </script> 
</head>
<body>
    <button onclick="GetSelector ()">Get the selector component!</button>
    <br /><br />
    <div class="red">red division</div>
    <div>non-red division</div>
    <span class="red">red span</span>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content