I've found myself in a situation where I'm given an NodeList that I'm trying to drop into a jQuery object to manipulate. The problem is that array also includes text nodes, comment nodes, etc, and seems to confuse jQuery when you just say $(nodeList)
.
I've used underscore to make a quick clean
function that strips out the DOM elements and returns them as an array:
_.mixin({
clean: function(nodeList) {
return _(nodeList).filter(function(node) {
return node.nodeType == 1;
});
}
});
with this $(_(nodeList).clean())
gives me a usable jQuery object.
Is there a better way to go about this?
Thanks.