canHaveChildren property
Retrieves a Boolean value that indicates whether the element can contain child elements.
If an element can have children, than you can add them with the insertBefore or appendChild methods.
To check whether an element can have HTML content, use the canHaveHTML property.
Syntax:
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:
Element cannot contain other elements. | |||||||
Element cancontain other elements. |
Default: this property has no default value.
Example HTML code 1:
The following example checks if an element can have children before inserting one:
|
||||
<head> <script type="text/javascript"> function AddChildren (id) { var span = document.createElement ("span"); span.style.color = "#FF0000"; span.innerHTML = " and some new content"; var elemToAdd = document.getElementById (id); if ('canHaveChildren' in elemToAdd) { if (elemToAdd.canHaveChildren) { elemToAdd.appendChild (span); } else { alert ("Element cannot have children."); } } else { alert ("Your browser does not support this example!"); } } </script> </head> <body> <div id="myDiv">The original contents</div> <input id="myInput" /> <button onclick="AddChildren ('myDiv');">Add children to the division</button> <button onclick="AddChildren ('myInput');">Add children to the input</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the use of the canHaveChildren 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?
|
Supported by objects:
CommentNode
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:reset, input:submit, input:text, ins, isindex, kbd, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments