Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific inline style with jquery

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/

like image 739
Evan Avatar asked Sep 29 '10 06:09

Evan


People also ask

How to remove inline style in jQuery?

Approach: The jQuery attr() and removeAttr() methods are used to remove the inline style property. The attr() method sets the attribute value to empty (”).

How do I remove inline styling?

To remove inline Styles from an existing page, open your page with the editor, select the paragraph (or the entire content by pressing CTRL + A) and then click on Remove Formatting button.

How to remove style jQuery?

To remove an attribute from each tag using jQuery, use the removeAttr() method and use the Universal Selector. Let us see how to use the method to remove all style attribute. Use the universal selector also to select all the elements.

How do you delete an element style?

You can remove CSS style properties from an element by setting the property to a null value, e.g. box. style. backgroundColor = null; . When an element's CSS property is set to null , the property is removed from the element.


2 Answers

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");​
like image 80
Brian Kim Avatar answered Nov 15 '22 10:11

Brian Kim


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. :)

like image 20
Nik Chankov Avatar answered Nov 15 '22 10:11

Nik Chankov