3

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 3

2
var all = document.getElementsByTagName("*");

for (var i=0, max=all.length; i < max; i++) {
     // Do something with the element here
     all[i].removeAttribute("style","")
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think he wants to do this using PHP.
Its better to process DOM on client side
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.
2

Using DOM is the simpliest way.

Here's what I would do.

  1. Load your markup using loadHTML().
  2. Select all elements with the styles attribute using DOMXPath.
  3. Remove the styles attribute using removeAttribute() on each DOMElement.

Comments

0

You need to go throguh every element on the page and call element.removeAttribute("style") You can recursivley traverse the dom using javascript, there are plenty of examples of this online.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.