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/
Approach: The jQuery attr() and removeAttr() methods are used to remove the inline style property. The attr() method sets the attribute value to empty (”).
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.
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.
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.
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");
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With