You are here: Reference > JavaScript > client-side > HTML DOM > properties > canHaveHTML

canHaveHTML property

Browser support:
Retrieves a Boolean value that indicates whether the element can contain HTML formatted text.
If the canHaveHTML property returns false, then do not write the innerHTML property. The canHaveHTML property is similar to the canHaveChildren property. When the canHaveChildren property returns false, then the canHaveHTML property also returns false, although some elements can have children, but cannot contain HTML formatted text. See the example below.

Syntax:

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

Possible values:

Boolean, one of the following values:
false
Element cannot contain HTML formatted text.
true
Element can contain HTML formatted text.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the canHaveHTML property:
<head>
    <script type="text/javascript">
        function Init () {
            var message = "";
            var content = document.getElementById ("content");

            if (('canHaveChildren' in content) && ('canHaveHTML' in content)) {
                var child = content.firstChild;
                while (child) {
                    if (child.tagName) {
                        if (child.canHaveChildren) {
                            message += child.tagName + " elements can have children.<br />";
                        }
                        else {
                            message += child.tagName + " elements cannot have children.<br />";
                        }

                        if (child.canHaveHTML) {
                            message += child.tagName + " elements can have HTML content.<br />";
                        }
                        else {
                            message += child.tagName + " elements cannot have HTML content.<br />";
                        }
                    }
                    child = child.nextSibling;
                }
            }
            else {
                message = "Your browser does  not support this example!";
            }

            var info = document.getElementById ("info");
            info.innerHTML = message;
        }

    </script>
</head>
<body onload="Init ()">
    canHaveChildren and canHaveHTML property test for different elements:
    <div id="content">
        <div id="info" style="background-color:#e0a0a0; width:300px"></div>
        <select>
            <option>first option</option>
            <option>second option</option>
        </select>
        <br />
        <button>Test button</button>
    </div>
</body>
Did you find this example helpful? yes no

Supported by objects:

External links:

User Contributed Comments

Post Content

Post Content