Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing html element styles via javascript

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. Anyone see any problems? Thanks in advance...


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"); } 
like image 835
danwoods Avatar asked Jun 24 '09 19:06

danwoods


People also ask

Can JavaScript remove HTML elements?

Approach: Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

Can JavaScript change HTML styles?

The HTML DOM allows JavaScript to change the style of HTML elements.

How do I get rid of 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

you can just do:

element.removeAttribute("style") 
like image 69
cloudhead Avatar answered Oct 03 '22 12:10

cloudhead


In JavaScript:

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

In jQuery:

$("#id").css('display',null); 
like image 34
Sergio Avatar answered Oct 03 '22 14:10

Sergio