Last active
August 29, 2015 14:22
-
-
Save iansvo/73629ce855ea8b15c6d6 to your computer and use it in GitHub Desktop.
Removes inline attributes from a list of HTML elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var element_array = [ | |
['.selector', true], | |
['.other-selector', false] | |
]; | |
var remove_attributes_array = [ | |
'style', | |
'align', | |
'border', | |
'cellpadding', | |
'cellspacing', | |
'width', | |
'height' | |
]; | |
function agnostize_element_styles(array) { | |
for(var i = 0; i < array.length; i++) | |
{ | |
var selector = array[i][0]; | |
var children_flag = array[i][1]; | |
var element_list = document.querySelectorAll(selector); | |
for(var i = 0; i < element_list.length; i++) | |
{ | |
inline_html_attributes( element_list[i], remove_attributes_array ); | |
if( children_flag === true && element_list[i].hasChildNodes() ) | |
{ | |
var child_nodes = element_list[i].querySelectorAll('*'); | |
for(var i = 0; i < child_nodes.length; i++) | |
{ | |
inline_html_attributes( child_nodes[i], remove_attributes_array ); | |
} | |
} | |
} | |
} | |
} | |
function inline_html_attributes(element, attribute_array) { | |
for(var i = 0; i < attribute_array.length; i++) | |
{ | |
element.removeAttribute(attribute_array[i]); | |
} | |
} | |
agnostize_element_styles(element_array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment