Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery to remove an inline style

Tags:

jquery

css

<div class="mydiv">
  <ul style="height:111px; width:222px;">
    ...

Is it possible with jQuery (or plain javascript) to remove just the height attribute? Note that there are several instances of .mydiv ul with varying heights.

like image 966
Scott Greenwald Avatar asked Jan 09 '12 15:01

Scott Greenwald


People also ask

How do I get rid of inline style?

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. Inline styles, while they have a purpose, are not the best way to maintain your Web site.

How we can remove CSS in jQuery?

To remove all CSS classes of an element, we use removeClass() method. The removeClass() method is used to remove one or more class names from the selected element.

How do I remove a single style property in CSS?

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.

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

To remove an attribute, use this:

$(".mydiv UL").css("height", "");

Thanks to am not i am for correcting my answer.

like image 108
Rory McCrossan Avatar answered Sep 28 '22 17:09

Rory McCrossan


Yes! You can!

If you set the css property to a blank value, it clears it.

So, if your inline style is "background: pink" you can remove it via:

$element.css('background','')

I'm pretty sure I learned that somewhere here on SE but, alas, don't have the specific source to site, so apologies for not being able to give credit where credit is due.

UPDATE:

Well, I suppose I should have gone to the source. From the jQuery documentation (emphasis mine):

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 element.

http://api.jquery.com/css/

like image 34
DA. Avatar answered Sep 28 '22 16:09

DA.