You are here: Reference > JavaScript > client-side > HTML DOM > properties > specified (attribute)

specified property (attribute)

Browser support:
Retrieves a Boolean value that indicates whether an attribute is specified.
The attributes collection contains only the specified attributes for an element in all browsers except in Internet Explorer before version 8. In Internet Explorer before version 8, the attributes collection contains all attributes that are supported by the current object.
Similarly, the getAttributeNode method returns null for non-specified attributes in all browsers except in Internet Explorer before version 8. In Internet Explorer before version 8, if a non-specified attribute has a default value, the getAttributeNode method returns a node that represents it.
If you want to detect whether an attribute with the given name is specified, use the getAttributeNode method or the attributes collection, and the specified property in Internet Explorer before version 8. In other browsers, the simplest solution is to use the hasAttribute method.

Syntax:

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

Possible values:

Boolean that represents the attribute state.
One of the following values:
false
Attribute is not specified.
true
Attribute is specified.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the specified property for iterating through the attributes of an element:
<head>
    <script type="text/javascript">
        function GetSpecifiedAttributes () {
            var message = "The following attributes specified for the body:\n";

            for (var i=0; i < document.body.attributes.length; i++) {
                var attr = document.body.attributes[i];
                if (attr.specified) {
                    message += "\n" + attr.nodeName + "=" + attr.nodeValue;
                }
            }

            alert (message);
        }
    </script> 
</head>
<body onload="GetSpecifiedAttributes ()" contentEditable="false">
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the specified property for a specified and a non-specified attribute:
<head>
    <script type="text/javascript">
        function TestAttribute (attrName) {
            var textInput = document.getElementById ("myInput");

            var typeAttr = textInput.getAttributeNode (attrName);
            if (typeAttr) {
                if (typeAttr.specified) {
                    alert ("The " + attrName + " attribute is specified and its value is " + typeAttr.nodeValue);
                }
                else {  // Internet Explorer
                    alert ("The " + attrName + " attribute is not specified, but it has the following default value: " + typeAttr.nodeValue);
                }
            }
            else {
                alert ("The " + attrName + " attribute is not specified for the input field.");
            }
        }
    </script> 
</head>
<body>
    <input id="myInput" value="Initial text" />

    <br /><br />
    <button onclick="TestAttribute ('type');">Test the 'type' attribute of the input above</button>
    <br />
    <button onclick="TestAttribute ('value');">Test the 'value' attribute of the input above</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content