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

setStringValue method (CSSPrimitiveValue)

Browser support:
Specifies a string value for the CSSPrimitiveValue object with the specified type.
If the style property that belongs to the CSSPrimitiveValue object cannot accept the new value, the setStringValue method raises an exception. A CSSPrimitiveValue object can only be retrieved with the getPropertyCSSValue method.
Two objects exist in JavaScript that support the getPropertyCSSValue method, the style and CSSStyleDeclaration objects. The CSSStyleDeclaration object is read-only, so you cannot use the setStringValue method on it. The style object is writable in Firefox, but the getPropertyCSSValue method always returns null on it.
Therefore, the setStringValue method can only be used on the style object in Google Chrome and Safari.

Syntax:

object.setStringValue (type, value);
You can find the related objects in the Supported by objects section below.

Parameters:

type
Required. Integer that specifies the type of the string.
Predefined constants are available for the possible values of this parameter, in the scope of the CSSPrimitiveValue object. For further details, see the page for the CSSPrimitiveValue object.
One of the following values (the value of a predefined constant appears in parentheses after the constant):
CSS_STRING (19)
The specified value is a simple string.
CSS_URI (20)
The specified value is a Uniform Resource Identifier.
CSS_IDENT (21)
The specified value is an identifier.
CSS_ATTR (22)
The specified value is an attribute function.
value
Required. String that specifies the value of the string.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the setStringValue method:
<head>
    <script type="text/javascript">
        function SetDisplay () {
            var div = document.getElementById ("myDiv");
            try {
                var cssValue = div.style.getPropertyCSSValue ("display");
                if (cssValue) {
                    cssValue.setStringValue (CSSPrimitiveValue.CSS_STRING, "none");
                    alert ("The value of the display property is changed.");
                }
                else {
                    alert ("Cannot access the value of the display property!");
                }
            } 
            catch (e) {
                alert ("Your browser does not support the getPropertyCSSValue method!");
            }               
        }
    </script>
</head>
<body>
    <div id="myDiv" style="display:block; background-color:#a0e0e0;">A div element</div>
    <br /><br />
    <button onclick="SetDisplay ()">Set the display property of the div to 'none'!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content