how to remove css property using javascript?

JavascriptCss

Javascript Problem Overview


is it possible to remove a CSS property of an element using JavaScript ? e.g. I have div.style.zoom = 1.2, now i want to remove the zoom property through JavaScript ?

Javascript Solutions


Solution 1 - Javascript

You have two options:

OPTION 1:

You can use removeProperty method. It will remove a style from an element.

el.style.removeProperty('zoom');

OPTION 2:

You can set it to the default value:

el.style.zoom = "";

The effective zoom will now be whatever follows from the definitions set in the stylesheets (through link and style tags). So this syntax will only modify the local style of this element.

Solution 2 - Javascript

removeProperty will remove a style from an element.

Example: > div.style.removeProperty('zoom');

MDN documentation page:
CSSStyleDeclaration.removeProperty

Solution 3 - Javascript

div.style.removeProperty('zoom');

Solution 4 - Javascript

element.style.height = null;

output:

<div style="height:100px;"> 
// results: 
<div style="">

Solution 5 - Javascript

You can use the styleSheets object:

document.styleSheets[0].cssRules[0].style.removeProperty("zoom");

Caveat #1: You have to know the index of your stylesheet and the index of your rule.

Caveat #2: This object is implemented inconsistently by the browsers; what works in one may not work in the others.

Solution 6 - Javascript

You can try finding all elements that have this class and setting the "zoom" property to "nothing".

If you are using jQuery javascript library, you can do it with $(".the_required_class").css("zoom","")

Edit: Removed this statement as it turned out to not be true, as pointed out in a comment and other answers it has indeed been possible since 2010.

False: there is no generally known way for modifying stylesheets from JavaScript.

Solution 7 - Javascript

You can also do this in jQuery by saying $(selector).css("zoom", "")

Solution 8 - Javascript

This should do the trick - setting the inline style to normal for zoom:

> $('div').attr("style", "zoom:normal;");

Solution 9 - Javascript

actually, if you already know the property, this will do it...

for example:

<a href="test.html" style="color:white;zoom:1.2" id="MyLink"></a>

    var txt = "";
    txt = getStyle(InterTabLink);
    setStyle(InterTabLink, txt.replace("zoom\:1\.2\;","");
    
    function setStyle(element, styleText){
        if(element.style.setAttribute)
	        element.style.setAttribute("cssText", styleText );
        else
	        element.setAttribute("style", styleText );
    }

    /* getStyle function */
    function getStyle(element){
        var styleText = element.getAttribute('style');
        if(styleText == null)
	        return "";
        if (typeof styleText == 'string') // !IE
	        return styleText;
        else  // IE
	        return styleText.cssText;
    } 

Note that this only works for inline styles... not styles you've specified through a class or something like that...

Other note: you may have to escape some characters in that replace statement, but you get the idea.

Solution 10 - Javascript

To change all classes for an element:

document.getElementById("ElementID").className = "CssClass";

To add an additional class to an element:

document.getElementById("ElementID").className += " CssClass";

To check if a class is already applied to an element:

if ( document.getElementById("ElementID").className.match(/(?:^|\s)CssClass(?!\S)/) )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsatView Question on Stackoverflow
Solution 1 - JavascriptRoland BoumanView Answer on Stackoverflow
Solution 2 - JavascriptEdvinas IlčenkoView Answer on Stackoverflow
Solution 3 - JavascriptjoeView Answer on Stackoverflow
Solution 4 - JavascriptT.ToduaView Answer on Stackoverflow
Solution 5 - Javascriptjames.garrissView Answer on Stackoverflow
Solution 6 - JavascriptnaivistsView Answer on Stackoverflow
Solution 7 - JavascriptTryingToImproveView Answer on Stackoverflow
Solution 8 - JavascriptMattView Answer on Stackoverflow
Solution 9 - JavascriptMaxOvrdrvView Answer on Stackoverflow
Solution 10 - JavascriptthejustvView Answer on Stackoverflow