Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Style on Element

I was wondering if this is possible.

There's this element

<div id="sample_id" style="width:100px; height:100px; color:red;"> 

So I want to remove width:100px; and height:100px;

the result would be

<div id="sample_id" style="color:red;"> 

Any help would be apprreciated. :)

like image 382
Wesley Brian Lachenal Avatar asked Sep 09 '13 04:09

Wesley Brian Lachenal


People also ask

How do I remove a style from an element?

Use the removeAttribute() method to remove all styles from an element, e.g. box. removeAttribute('style') . The removeAttribute method will remove the style attribute from the element.

How do you remove an element style in CSS?

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 can edit style with pure Javascript. No library needed, supported by all browsers except IE where you need to set to '' instead of null (see comments).

var element = document.getElementById('sample_id');  element.style.width = null; element.style.height = null; 

For more information, you can refer to HTMLElement.style documentation on MDN.

like image 99
Blackus Avatar answered Oct 03 '22 07:10

Blackus


var element = document.getElementById('sample_id'); element.style.removeProperty("width"); element.style.removeProperty("height"); 
like image 26
madhu sudhanan Avatar answered Oct 03 '22 07:10

madhu sudhanan