You are here: Reference > JavaScript > client-side > HTML DOM > methods > hasAttributes

hasAttributes method

Browser support:
8
Returns whether the current element has any attributes specified or not.
Note: The hasAttributes method is supported in Internet Explorer from version 8.
In earlier versions of Internet Explorer, use the specified property of the attribute objects contained by the attributes collection of the current element to detect the specified attributes. For further details, see Example 1 below.

Syntax:

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

Return value:

Boolean. One of the following values:
false No attribute is specified for the current element.
true At least one attribute is specified for the current element.

Example HTML code 1:

This example illustrates the use of the hasAttributes method:
<head>
    <script type="text/javascript">
        function HasAttr (button) {
            var hasAttrs = false;
            if (button.hasAttributes) {
                hasAttrs = button.hasAttributes ();
            }
            else {  // Internet Explorer before version 8
                var attrs = button.attributes;
                for (var i = 0; i < attrs.length; i++) {
                    if (attrs[i].specified) {
                        hasAttrs = true;
                        break;
                    }
                }
            }
            if (hasAttrs) {
                alert ("At least one attribute is specified for the button.");
            }
            else {
                alert ("No attribute is specified for the button.");
            }
        }
    </script>
</head>
<body>
    <button onclick="HasAttr (this)">Does it have any attributes or not?</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content