This is the function I use for that exact thing:
$.fn.removeStyle = function(name) {
// Convert the style name to a lower-case string array.
if (!name)
return this;
else if (name.toLowerCase)
name = [ name.toLowerCase() ];
else if (name.length)
{
for (var i=0;i<name.length;i++)
name[i] = (''+name[i]).toLowerCase();
}
else
return this;
// Remove the styles from each of the elements.
return this.each(function(i) {
var el = $(this);
var styles = (el.attr('style')||'').split(';');
for (var i=styles.length-1;i>=0;i--)
{
var style = $.trim(styles[i].split(':')[0]).toLowerCase();
for (var j=0;j<name.length;j++)
{
// If we found a matching style (or it matches a meta-style).
if (style==name[j] || style.indexOf(name[j]+'-')==0)
{
// Remove it.
if (i==styles.length-1)
styles.pop();
else if (i==0)
styles.shift();
else
styles.splice(i,i);
break;
}
}
}
// Assign the changed style.
var newstyles = styles.join(';');
if (newstyles)
el.attr('style',newstyles);
else
el.removeAttr('style');
});
}
-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of Ricardo Tomasi
Sent: Sunday, December 14, 2008 2:46 PM
To: jQuery Development
Subject: [jquery-dev] Re: Feature request: Delete a CSS style value
When I started reading I thought you were talking about deleting the
CSS in the stylesheet, thus modifying it, not overwriting it with more
specific rules (style att). That would be interesting.
On Dec 12, 2:42 pm, jez9999 <[email protected]> wrote:
> Hello,
>
> I'd like to see the following in the next version of jQuery: a
> function named something like .delCss(name), which would actually
> delete/unset the style property of the given name on all matched
> elements.
>
> See, you can already do this by setting the desired style properties/
> values in a particular class and then using .removeClass(classname),
> so it seems to me to be logical that you should be able to do it with
> CSS style properties too.
>
> I know common things to do are to set properties to the empty string,
> or 'default', or something instead, but logically to me it feels nicer
> to be able to 'unset' the property, even if what jQuery is doing
> behind the scenes is just setting the property to the empty string.
> Perhaps in future browsers will offer a genuine way to unset
> properties in Javascript using an actual Javascrupt 'unset' function,
> which could then be implemented in jQuery's .delCss() function.
>
> An example of how this would work would be, say, I set a particular
> background-color on an element (either using style="" in the markup or
> via Javascript, which I understand is identical in terms of
> specificity). I could then later call $('#myElem').delCss('background-
> color') on it, which in this property's case would cause the browser
> to revert it to the default when the property is unspecified -
> 'transparent'
(seehttp://www.eskimo.com/~bloo/indexdot/css/properties/colorbg/bgcolor.htm)
.
> This feels nicer to me than $('#myElem').css('background-color', '').
> Could we see this in future? :-)
>
> Best regards,
> Jeremy Morton (Jez)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---