Make a tree object from an element

(function () { UIUtility = { toTreeObject: function(element) { var jsTree = { _: element, _children : [] }; var indexIndicator = {}; for (var index in element.childNodes) { var node = element.childNodes[index]; if(node.nodeType === Node.ELEMENT_NODE) { var key = node.nodeName.toLowerCase(); if(indexIndicator[key]) { indexIndicator[key]++; jsTree[key + '_' + indexIndicator[key]] = UIUtility.toTreeObject(node); } else { indexIndicator[key] = 1; jsTree[node.nodeName.toLowerCase()] = UIUtility.toTreeObject(node); } jsTree._children.push(node); } } return jsTree; } }; })();
See the example at: https://jsfiddle.net/oc9op9dj/2/

UIUtility.toTreeObject creates a tree object from the passed element and its children so access them easily

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.