You are here: Reference > JavaScript > client-side > style handling > objects > Counter

Counter object

Browser support:
Represents a style value when it is a counter function.
If the value of a style property is a counter function, you can use the getCounterValue method to retrieve a Counter object that represents the value.
Calling the getCounterValue method is the only way to retrieve a Counter object, but the getCounterValue method is not implemented by browsers and raises an exception in Firefox (see the example below).

Syntax:

Methods that return the object:
CSSPrimitiveValue.getCounterValue ( )
The base interface, through which you can add new functionalities to the Counter object, is the Counter interface.

Possible members:

Properties:
identifier
Returns a string that identifies the name of the counter.

This property is read-only.
listStyle
Returns a string that identifies the list style of the counter.

This property is read-only.
separator
Returns a string that identifies the separator of the nested counters.

This property is read-only.

Example HTML code 1:

This example shows how to use the Counter object for the value of the content style property:
<head>
    <script type="text/javascript">
        function GetCounter () {
            var div = document.getElementById ("myDiv");
            if (window.getComputedStyle) {
                var compStyle = window.getComputedStyle (div, null);
                try {
                    var value = compStyle.getPropertyCSSValue ("content");
                    try {
                        var counterValue = null;
                        if (value) {
                            if (value.cssValueType == CSSValue.CSS_PRIMITIVE_VALUE) {
                                if (value.primitiveType == CSSPrimitiveValue.CSS_COUNTER) {
                                    counterValue = value.getCounterValue ();
                                }
                            }
                            else {
                                if (value.cssValueType == CSSValue.CSS_VALUE_LIST) {
                                    if (value.length > 0) {
                                        if (value[0].primitiveType == CSSPrimitiveValue.CSS_COUNTER) {
                                            counterValue = value[0].getCounterValue ();
                                        }
                                    }
                                }
                            }
                        }

                        if (counterValue) {
                            alert ("(identifier: " + counterValue.identifier + 
                                   ", listStyle: " + counterValue.listStyle + 
                                   ", separator: " + counterValue.separator + ")");
                        }
                        else {
                            alert ("The value of the counter-increment property is not a counter function!");
                        }
                    }
                    catch (e) {
                        alert ("Your browser does not support the getCounterValue method!");
                    }
                } 
                catch (e) {
                    alert ("Your browser does not support the getPropertyCSSValue method!");
                }
            }
            else {
                alert ("Your browser does not support the getComputedStyle method!");
            }
        }
    </script>
</head>
<body>
    <div id="myDiv" style="content: counter(chapter);">An element with style="content: counter(chapter);"</div>
    <br />
    <button onclick="GetCounter ()">Get the counter value of the content property!</button>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content