function enableDescendants(obj, isEnable) { if (obj == null) return; let children = obj.childNodes; for (let i = 0; i < children.length; i++) { if (children[i].nodeType == 1) { // IE only allows elements children[i].disabled = !isEnable; } enableDescendants(children[i], isEnable); } } // Examples use function exampleDisable() { let obj = document.getElementById('my-element'); enableDescendants(obj, false); } function exampleEnable() { let obj = document.getElementById('my-element'); enableDescendants(obj, true); }
Programming Tips - javaScript: enable/disable all children and children's children of a node
Date: 2009jan23
Updated: 2023nov4
Language: javaScript
Keywords: descendants, enabled, disable, recursive
Q. javaScript: enable/disable all children and children's children of a node
A. This function does just that: