Dave's Brain

Browse - programming tips - javascript disable descendants

Date: 2009jan23
Language: javaScript

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);
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, 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.