You are here: Reference > JavaScript > client-side > style handling > methods > getRectValue (CSSPrimitiveValue)

getRectValue method (CSSPrimitiveValue)

Browser support:
If the value represented by the current CSSPrimitiveValue object is a rect function, then retrieves a CSSPrimitiveValue object that represents the value.
This method can be used if the type of the represented value is CSS_RECT. To get the type of the represented value, use the primitiveType property.

Syntax:

object.getRectValue ( );
You can find the related objects in the Supported by objects section below.

Return value:

Returns a CSSPrimitiveValue object that represents the value.

Example HTML code 1:

This example illustrates the use of the getRectValue method:
<head>
    <style>
        #testDiv {
            position: absolute;
            top:20px;
            left:30px;
            background-color: yellow; 
            width:200px;
            height:400px;
            padding-top:30px;
            padding-left:10px;
            clip: rect(25px, 150px, 150px, 5px);
        }
    </style>
    <script type="text/javascript">
        function GetClipRect () {
            var div = document.getElementById ("testDiv");
            if (window.getComputedStyle) {
                var compStyle = window.getComputedStyle (div, null);
                try {
                    var value = compStyle.getPropertyCSSValue ("clip");
                    if (value) {
                        var valueType = value.primitiveType;
                        if (valueType == CSSPrimitiveValue.CSS_RECT) {
                            var rect = value.getRectValue ();
                            var topPX = rect.top.getFloatValue (CSSPrimitiveValue.CSS_PX);
                            var rightPX = rect.right.getFloatValue (CSSPrimitiveValue.CSS_PX);
                            var bottomPX = rect.bottom.getFloatValue (CSSPrimitiveValue.CSS_PX);
                            var leftPX = rect.left.getFloatValue (CSSPrimitiveValue.CSS_PX);
                            
                            message = "left: " + leftPX + "px\n";
                            message += "top: " + topPX + "px\n";
                            message += "right: " + rightPX + "px\n";
                            message += "bottom: " + bottomPX + "px";
                            alert (message);
                        }
                        else {
                            alert ("The value of the clip property is not a rect function!");
                        }
                    }
                    else {
                        alert ("Your browser does not support the getPropertyCSSValue method for the clip property!");
                    }
                } 
                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="testDiv">An element with clip.</div>
    <br />
    <button onclick="GetClipRect ()">Get the rectangle specified by the clip style property!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content