100

I'm trying to replace an element's inline style tag value. The current element looks like this:

`<tr class="row-even" style="background: red none repeat scroll 0% 0%; position: relative; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="0000ph2009-06-10s1s02">`

and I'd like to remove all that style stuff so that it's styled by it's class rather than it's inline style. I've tried delete element.style; and element.style = null; and element.style = ""; to no avail. My current code breaks at these statement. The whole function looks like:
function unSetHighlight(index){

if(index < 10)
index = "000" + (index);
else if (index < 100)
  index = "000" + (index);
  else if(index < 1000)
    index = "0" + (index);
    if(index >= 1000)
      index = index;
    
var mainElm = document.getElementById('active_playlist');
var elmIndex = "";

for(var currElm = mainElm.firstChild; currElm !== null; currElm = currElm.nextSibling){
  if(currElm.nodeType === 1){

  var elementId = currElm.getAttribute("id");

  if(elementId.match(/\b\d{4}/)){
    
    elmIndex = elementId.substr(0,4);
    
            
    if(elmIndex == index){
      var that = currElm;
      //that.style.background = position: relative;
      }
    }
  }
}
       
clearInterval(highlight);
alert("cleared Interval");
that.style.background = null;

alert("unSet highlight called");
}

the clearInterval works but the alert never fires and the background stays the same. What's the problem?


function unSetHighlight(index){  
  alert(index);  
  if(index < 10)  
    index = "000" + (index);  
    else if (index < 100)  
      index = "000" + (index);  
      else if(index < 1000)  
        index = "0" + (index);  
        if(index >= 1000)  
          index = index;  
        
    var mainElm = document.getElementById('active_playlist');
    var elmIndex = "";
  
    for(var currElm = mainElm.firstChild; currElm !== null; currElm = currElm.nextSibling){
      if(currElm.nodeType === 1){

      var elementId = currElm.getAttribute("id");

      if(elementId.match(/\b\d{4}/)){
        
        elmIndex = elementId.substr(0,4);
        alert("elmIndex = " + elmIndex + "index = " + index);
        
                
        if(elmIndex === index){
          var that = currElm;
          alert("match found");
          }
        }
      }
    }
        
    clearInterval(highlight);
    alert("cleared Interval");
    that.removeAttribute("style");
    
    //that.style.position = "relative";
    //reColor();
    alert("unSet highlight called");
}
1
  • Tried removeAttribute("style"), still no luck. I'm using setInterval to cycle through background colors to (try to) create a pulse effect. I'll try writing up some other style classes to try answer 4. Any other ideas? My current code is posted below... Commented Jun 24, 2009 at 20:53

8 Answers 8

200

you can just do:

element.removeAttribute("style")
Sign up to request clarification or add additional context in comments.

3 Comments

Or element.style.cssText = null, which does much the same.
@mrec not exactly the same, in your case an element still has empty style attribute
This answers the OP's question -- remove all inline style and fallback to stylesheet rules, but...it also blows away all the inline styles. If you want to selectively remove rules from the inline styles, @sergio's answer below (actually, davidjb's comment on that answer) is more useful.
77

In JavaScript:

document.getElementById("id").style.display = null;

In jQuery:

$("#id").css('display',null);

5 Comments

The first line of code appears to either error in Internet Explorer (<9) with Invalid argument or cause the element to disappear entirely in IE >= 9. Setting getElementById("id").style.display = '', being the empty string, appears to work across browsers.
in jQuery the correct way to remove a style is $("#id").css('display', '');
This got me close, but it wasn't null, but 'none' that worked, e.g. $("#id").css('display','none');
display:none has nothing to do with this question or answer. That's for hiding elements.
There is also CSSStyleDeclaration.removeProperty() which imho is a lot cleaner than a null assignment. See developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/…
12
getElementById("id").removeAttribute("style");

if you are using jQuery then

$("#id").removeClass("classname");

1 Comment

Those two code snippets do wildly different things. Only the first has anything to do with the question.
5

The class attribute can contain multiple styles, so you could specify it as

<tr class="row-even highlight">

and do string manipulation to remove 'highlight' from element.className

element.className=element.className.replace('hightlight','');

Using jQuery would make this simpler as you have the methods

$("#id").addClass("highlight");
$("#id").removeClass("hightlight");

that would enable you to toggle highlighting easily

3 Comments

Another advantage of defining .highlight in your style sheet is you can change exactly how a "highlight" is displayed by just changing one line of CSS and not making any Javascript changes at all. If you're not using JQuery, adding and removing a class is still pretty simple: /*on*/ theElement.className += ' highlight'; /*off*/ theElement.className = theElement.className.replace(/\b\s*highlight\b/, ''); (By defining .element in CSS, it automatically stays the same everywhere, too.)
Oops, forgot possibility class is specified more than once. To handle that case too, add 'g' flag to the regular expression: theElement.className = theElement.className.replace(/\/b\s*highlight\b/g, '');
use classlist node property instead of className
5

Remove removeProperty

var el = document.getElementById("id");
el.style.removeProperty('display')

console.log("display removed" + el.style["display"])
console.log("color " + el.style["color"])
<div id="id" style="display:block;color:red">s</div>

Comments

4

Use

particular_node.classList.remove("<name-of-class>")

For native javascript

Comments

2

In jQuery, you can use

$(".className").attr("style","");

Comments

2

Completly removing style, not only set to NULL

document.getElementById("id").removeAttribute("style")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.