How can I use Javascript to loop through HTML elements and remove inline attributes? -
i working online store platform doesn't provide full editing access html on page , looking vanilla js solution remove various inline styling can't remove directly.
i interested in reusable function such purpose, use type of capability on project later.
my solution involved creation of 2 dimensional array , 2 helper functions. hope else finds useful on 1 of projects:
step 1 - setup element/node list
the first part of fill application elements want work with. nested arrays have 2 values: css selector element in question, , boolean (true/false) flag indicate if element's children should have attributes removed.
var element_array = [ ['.selector', true], ['.other-selector', false] ];
step 2: setup inline attribute array
the next step indicate html attributes want remove each element. each entered array:
var remove_attributes_array = [ 'style', 'align', 'border', 'cellpadding', 'cellspacing', 'width', 'height' ];
step 3: setup helper functions
the helper functions perform logic of searching through elements , removing inline styles.
function 1: agnostize_element_styles(array);
this function loops through element_array variable (array literal step 1) , executes second function (which removes attributes). if 2nd array value true
, element's children iterated on , inline attributes removed.
function source
function agnostize_element_styles(array) { for(var = 0; < array.length; i++) { var element_list = document.queryselectorall(array[i][0]); for(var = 0; < element_list.length; i++) { inline_html_attributes( element_list[i], remove_attributes_array ); if( array[i][1] === true && element_list[i].haschildnodes() ) { var child_nodes = element_list[i].queryselectorall('*'); for(var = 0; < child_nodes.length; i++) { inline_html_attributes( child_nodes[i], remove_attributes_array ); } } } } }
function 2: inline_html_attributes(element);
this function accepts element argument, removes inline attributes specified in step 2's array variable.
function source:
function inline_html_attributes(element, attribute_array) { for(var = 0; < attribute_array.length; i++) { element.removeattribute(attribute_array[i]); } }
step 5: call function 1 using step 1 array
to make of work, run first function (agnostize_element_styles) passing in 2 dimensional array step 1 argument:
agnostize_element_styles(element_array);
view full code
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 = 0; < array.length; i++) { var element_list = document.queryselectorall(array[i][0]); for(var = 0; < element_list.length; i++) { inline_html_attributes( element_list[i], remove_attributes_array ); if( array[i][1] === true && element_list[i].haschildnodes() ) { var child_nodes = element_list[i].queryselectorall('*'); for(var = 0; < child_nodes.length; i++) { inline_html_attributes( child_nodes[i], remove_attributes_array ); } } } } } function inline_html_attributes(element, attribute_array) { for(var = 0; < attribute_array.length; i++) { element.removeattribute(attribute_array[i]); } } agnostize_element_styles(element_array);
Comments
Post a Comment