I'm trying to traverse a DOM object. Furthermore, this DOM object is the result of $.parseHTML(). My code makes sense but it's not working so I must not be considering something. Any help would be greatly appreciated?
-Geates
Code:
var str = "<div id='one'><div id='header'><b>hello</b></div><img src='a.gif'></div>";
var strElements = "";
var objDOM = $.parseHTML(str);
function traverseDOM(objDOM) {
$.each(objDOM, function (i, element) {
if (element.children().length > 0)
strElements += traverseDOM(element);
else
strElements += 'index: ' + i + ' - ' + 'node: ' + element.nodeName + '<br>';
});
return strElements;
}
alert(traverseDOM(objDOM));
-Geates