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

clearAttributes method

Browser support:
Clears all attributes from the current element, except the id, name, style, value and event attributes.

Syntax:

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

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the clearAttributes method:
<head>
    <script type="text/javascript">
        function ClearAttrs () {
            var marquee = document.getElementById ("myMarquee");
            if (marquee.clearAttributes) {
                marquee.clearAttributes ();
            } 
            else {
                for (var i = marquee.attributes.length - 1; i >= 0; i--) {
                    var attr = marquee.attributes[i];
                    if (attr.name.toLowerCase () == "id") {
                        continue;
                    }
                    if (attr.name.toLowerCase () == "style") {
                        continue;
                    }
                    if (attr.name.substring (0,2).toLowerCase () == "on") {
                        continue;
                    }
                    marquee.removeAttribute (attr.name, 0);
                }
            }
        }
    </script>
</head>
<body>
    <marquee id="myMarquee" bgcolor="red" onclick="alert ('Click event occurred!')" style="border:2px solid blue;">I am a marquee!</marquee>
    <br /><br />
    <button onclick="ClearAttrs ();">Remove the attributes of the marquee.</button>
    <br />
    Note: only the bgcolor attribute of the marquee can be removed with the button above.
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content