0

I tries to use this function:

Here is the full html:

<html>
   <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
   </head>
   <body>
     <div id="test" style="width:auto;max-width:150px;"></div>
     <script>
       (function($)
       {
           $.fn.removeStyle = function(style)
           {
               var search = new RegExp(style + '[^;]+;?', 'g');

               return this.each(function()
               {
                   $(this).attr('style', function(i, style)
                   {
                       return style.replace(search, '');
                   });
               });
           };
       }(jQuery));
       $(function(){
         $('#test').removeStyle('width');
       });
     </script>
   </body>
</html>

But I have get this problem:

<div id="test" style="max-"></div>
Community
  • 1
  • 1
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135

6 Answers6

3

To do it the way you've posted: just modify your regex to match style, but don't match -style.

To do it the right way: you can simply use:

$('#test').css('width', ''); 

Here is the simple fiddle which demonstrates use of .css() to remove background.

Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
2

I think you don't want to remove all styles, just the width: auto;

The regex matches *width:*, so it matches max-width: 150px;.

Try this instead:

var search = new RegExp('(^|;)' + style + '[^;]+;?', 'g');

This regex will expects a) the beginning of the string or b) a semicolon before it matches another part of the string.

See also here: http://jsfiddle.net/P5dfz/

Andy
  • 141
  • 1
  • 7
1

You should use jQuery's .css() to manipulate styles:

$('#test').css("width","");
Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
1
(function($)
   {
       $.fn.removeStyle = function(style)
       {
           var search = new RegExp(style + '[^;]+;?', 'g');

           return this.each(function()
           { 
               $(this).css(style,'');
               /*$(this).attr('style', function(i, style)
               { 
                   return style.replace(search, '');
               });*/
           });
       };
   }(jQuery));
   $(function(){
     $('#test').removeStyle('width');
   });

Modified your function please see fiddle http://jsfiddle.net/UA9CF/

Asif
  • 647
  • 9
  • 18
0

If you want to remove complete inline style attribute then you can simple use this.

$('#test').removeAttr('style');
Peeyush
  • 4,728
  • 16
  • 64
  • 92
0

but you asked, how to remove inline-style, so if you want to remove the entire style tag from certain elements in a given scope you can try out something like below:

say your markup is like this:

<div id="parent">
   <div style="background-color:red; width:100px; height:100px" >
      <div style="background-color:green; width:50px; height:50px" >
      </div>
   </div>
</div>
<div style="background-color:#CCC; width:50px; height:50px" >
</div>

and you want to remove style tags of the elements inside the div with id parent:

$('div',"#parent").removeAttr('style'); //$(selector, scope)

or something even simple:

$("#parent").find('div,a').removeAttr('style'); //div,a or other element types
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59