Using .filter()
, .some()
and .forEach()
should do the trick.
var els = document.querySelectorAll(".container *");
[].filter.call(els, function(el) {
return ![].some.call(el.attributes, function(attr) {
return /^data/i.test(attr.name);
});
}).forEach(function(el) {
el.parentNode.removeChild(el);
});
You'll need patches for old browsers, but you probably knew that already.
If like me, you like reusable functions:
var els = document.querySelectorAll(".container *");
[].filter.call(els, els_with_no_data)
.forEach(remove_nodes);
function remove_nodes(el) {
el.parentNode.removeChild(el);
}
function has_data_attrs(attr) {
return /^data/i.test(attr.name);
}
function els_with_no_data(el) {
return ![].some.call(el.attributes, has_data_attrs)
}
And then using Array generics (in supported browsers, otherwise polyfiilled):
var els = document.querySelectorAll(".container *");
Array.filter(els, els_with_no_data)
.forEach(remove_nodes);
function remove_nodes(el) {
el.parentNode.removeChild(el);
}
function has_data_attrs(attr) {
return /^data/i.test(attr.name);
}
function els_with_no_data(el) {
return !Array.some(el.attributes, has_data_attrs)
}