1

Possible Duplicates:
php regexp: remove all attributes from an html tag
Remove style attribute from HTML tags

EDITED:

For example i am having the following content:

    <div style="text-indent: -76.5pt; margin: 0in 54.9pt 0pt 76.5pt;"><strong>Motion with Constant Acceleration</strong></div>

I need to remove all style attributes using jQuery and PHP

My output should look like this:

    <div style=""><strong>Motion with Constant Acceleration</strong></div>

OR

    <div><strong>Motion with Constant Acceleration</strong></div>

How can this be done.. Any help will be thankful and grateful...

Thanks in advance...

Community
  • 1
  • 1
Fero
  • 12,969
  • 46
  • 116
  • 157
  • red pill or blue pill (jQuery or PHP) make your choice. – dynamic Jun 06 '11 at 09:38
  • it really depends if you want to do it in php or jquery. In PHP you could use preg_replace to remove the style attribute ... whilst in jQuery you could use .removeAttr("style"); – Gabriel Spiteri Jun 06 '11 at 09:39
  • I need know how it can be done using both of it... Shall any one explain me... – Fero Jun 06 '11 at 09:43

2 Answers2

13

jQuery option would be

$('*[style]').attr('style', ''); 
// sets style attribute to empty string on all elements that have it

PHP is referred in comments

mkilmanas
  • 3,395
  • 17
  • 27
7

Use not selector to exclude table

$("*[style]").not("table").removeAttr("style");

or

$("*[style]").not("table").attr("style", "");
Jith
  • 1,701
  • 3
  • 16
  • 22
  • 1
    Even though your version is completely valid, I think using negation in the selector makes it more concise and comprehensible. I.e. `$("*[style]:not(table)").removeAttr("style");` – mkilmanas Jun 06 '11 at 11:18