If you don't mind using XPath, here is my solution:
function removeMyDataAttributes() { var list, iterator, attribute, i, n; // Select all attribute nodes that it name starts with "data-myplugin-" iterator = document.evaluate('//*/attribute::*[starts-with(local-name(), "data-myplugin-")]', document.documentElement, null, XPathResult.ANY_TYPE, null); list = []; while ((attribute = iterator.iterateNext())) { list.push(attribute); // Note: can't call removeAttributeNode here: // InvalidStateError: Failed to execute 'iterateNext' on 'XPathResult': The document has mutated since the result was returned. } for (i = 0, n = list.length; i < n; ++i) { attribute = list[ i ]; attribute.ownerElement.removeAttributeNode(attribute); } }
source share