Dave's Brain

Browse - Programming Tips - How can I enable/disable all children and children's children of a node?

Date: 2009jan23
Language: javaScript
Keywords: descendants, enabled, disable

Q.  How can I enable/disable all children and children's children of a node?

A.  This function does just that:

function enableDescendants(obj, is_enable) { if (obj == null) return; var children = obj.childNodes; for (var i = 0; i < children.length; i++) { if (children[i].nodeType == 1) { // IE only allows elements children[i].disabled = !is_enable; } enableDescendants(children[i], is_enable); } } // Examples... function exampleDisable() { var obj = document.getElementById('my_element'); enableDescendants(obj, false); } function exampleEnable() { var obj = document.getElementById('my_element'); enableDescendants(obj, true); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.