I have a dom and in this i have loaded whole of the webpage html. I want to remove all the inline style from all the elements in this page. How can i do this.
3 Answers
var all = document.getElementsByTagName("*");
for (var i=0, max=all.length; i < max; i++) {
// Do something with the element here
all[i].removeAttribute("style","")
}
3 Comments
F21
I think he wants to do this using PHP.
Rab Khan
Its better to process DOM on client side
Emile Bergeron
What if the client's browser has js disabled? What if I wanted to remove the style as a server side validation of the data inputted by the client? It's not better to process DOM on client side, it depends on what you aim to do.
Using DOM is the simpliest way.
Here's what I would do.
- Load your markup using loadHTML().
- Select all elements with the
stylesattribute using DOMXPath. - Remove the styles attribute using removeAttribute() on each DOMElement.