views:

44

answers:

3

There's an asp menu I'm using that is auto inserting style="width:3px;" into my menu table tds creating a nasty gab in between my tabs. I'm testing to remove this inline style with jquery instead of our developer customizing the menu just for this cosmetic blemish.

below is a simple example:

<table border="1" cellspacing="0" cellpadding="0">
   <tr>
      <td style="width:3px;">hello world</td>
   </tr>
</table>

in jquery, i can remove all tds with the style attribute by doing:

$('td').removeAttr('style');

so, my question is, how can i target the inline style that only contains 3px?

Here's my live demo: http://jsfiddle.net/n9upF/

+2  A: 

so, my question is, how can i only target the inline style that contains only 3px?

Try this:

$('td').each(function(){
  if ($(this).attr('style').indexOf('3px') !== -1){
    $(this).removeAttr('style');
  }
});

See Working Example

Sarfraz
this is great if i need to kill all styles. thank you
Evan
+3  A: 

You are asking how you can select td's that have style attribute with only width:3px; in it, right?

You can use Attribute Equals Selector.

$("td[style='width:3px;']").removeAttr("style");​
Brian Kim
this did the trick very simply. thank you brian!
Evan
+1: I certainly missed that :)
Sarfraz
+1  A: 

I believe that Evan wanted to remove only the width:3px; from the style while the other css styling remain in the attribute. So here is the solution:

$('td').each(function(){
  if ($(this).attr('style').indexOf('3px') !== -1){
    var style = $(this).attr('style');
      $(this).attr('style', style.replace(/width: ?3px;/g, ''));
  }
});

Working example is here

If this is not what you needed then the Sarfraz is shown the proper solution. :)

Nik
nik - thank you for your help! i found Brian's seem to do the trick with a little less code, while yours addressed my question with more effort after Sarfaz started the code. again, i appreciate your help.
Evan
My solution would work if you want to remove only the width and keep the other styling. If you just want to remove the style attribute completely the solution of Brian is the shortest indeed. Although It didn't work for me. Anyway :)
Nik