Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unset an element's CSS attribute using jQuery?

Tags:

jquery

css

If I set a CSS value on a specific element using:

$('#element').css('background-color', '#ccc'); 

I want to be able to unset that element-specific value and use the cascaded value, along the lines of:

$('#element').css('background-color', null); 

But that syntax doesn't seem to work – is this possible using another syntax?

Edit: The value isn't inherited from the parent element – the original values comes from an element-level selector. Sorry for any confusion!

like image 372
cdleary Avatar asked Jan 29 '09 08:01

cdleary


People also ask

How we can remove CSS in jQuery?

removeClass(). removeAttr('style');

How do I remove an existing CSS property?

There is a property called all that is being proposed for resetting all CSS properties for a given element to certain CSS-wide values - the value you want to use would be unset , which resets a property to either its inherited value if it inherits by default, or otherwise, its initial value.

Which methods are used for removing any element or content in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

How do I remove a style tag?

If you want to remove a specific style tag, you can add a class (or id) to it and then use remove() to remove that class.


2 Answers

From the jQuery docs:

Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or <style> element.

like image 126
Glenn Moss Avatar answered Sep 20 '22 23:09

Glenn Moss


I think you can also do:

$('#element').css('background-color', ''); 

That's what I used to do a long time ago for the display property in plain-old-javascript to show/hide a field.

like image 24
FryGuy Avatar answered Sep 19 '22 23:09

FryGuy