-
removeAttribute in IE
Hi all,
I have the following structure in my HTML file:
HTML Code:
<TABLE ALIGN="left" class="altcolor" cellpadding="4">
<TR><TD align="center" ><a class="extlink" href="...">Some text</a>
<BR>
<TABLE WIDTH="475" ALIGN="Center" CELLPADDING="5" CELLSPACING="0">
etc...
I need to remove the WIDTH attribute from the nested table.
I have been able to get a reference to the table with the "altcolor" class (the first one).
I named the reference "alttable". So I tried this:
Code:
var table2 = alttable.getElementsByTagName("TABLE")[0];
table2.removeAttribute("WIDTH");
This code is located at the end of the HTML (in script tags)
This works in Firefox, but no luck in IE6 or IE7.
I am a noob when it comes to JS, so I was hoping someone could point out my mistake.
Thanks,
Adam
-
I think you're probably better off with table2.width = ''. IE doesn't support removeAttribute very well.
-
Hi Raffles,
Thanks for your reply. I tried that with no success as well. Just to make sure I had the right element, I inserted a conditional statement to test if the width attribute of table2 was equal to 475, which tested as true.
So I know I have the right element. There must be something else. Should I insert this code somewhere other than the end of the HTML document?
Thanks,
Adam
-
Note that the attribute might be case-sensitive, so table.width = ''; should be table.WIDTH = '';.
I'm not sure about it though, you should check it.
-
Hi Ize,
I appreciate your reply. I tried uppercase with no luck, still.
Thanks,
Adam
-
Does it throw an error in IE or do you not get the expected results?
-
Hi Jim,
No error. I just don't get the expected results in IE. Nothing happens.
Thanks,
Adam
-
I appreciate everyone's help so far.
Here is the entirety of the javascript at the bottom of my page. The getElementsByClassName function is one I got on Robert Nyman's site, that seems to work very well.
The first two lines, which replace the current classname with "feature" works great in IE an FF.
Hope this helps. Thanks.
Code:
<script type="text/javascript">
function getElementsByClassName(oElm, strTagName, oClassNames){
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
var arrRegExpClassNames = new Array();
if(typeof oClassNames == "object"){
for(var i=0; i<oClassNames.length; i++){
arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames[i].replace(/-/g, "\-") + "(\s|$)"));
}
}
else{
arrRegExpClassNames.push(new RegExp("(^|\s)" + oClassNames.replace(/-/g, "\-") + "(\s|$)"));
}
var oElement;
var bMatchesAll;
for(var j=0; j<arrElements.length; j++){
oElement = arrElements[j];
bMatchesAll = true;
for(var k=0; k<arrRegExpClassNames.length; k++){
if(!arrRegExpClassNames[k].test(oElement.className)){
bMatchesAll = false;
break;
}
}
if(bMatchesAll){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}
var feature = getElementsByClassName(document,"a","super")[0];
feature.className = "feature";
var mytable = getElementsByClassName(document, "table", "altcolor")[0];
mytable.removeAttribute("ALIGN");
mytable.setAttribute("align", "center")
var table2 = mytable.getElementsByTagName("TABLE")[0];
if (table2.hasAttribute("WIDTH"))
{
table2.removeAttribute("WIDTH");
}
</script>
-
Kind of amusing.. this works in IE6:
table2.removeAttribute("width");
(lowercase)
-
.......
You are right, Jim.
It looks like the problem was with the if statement before that. It evaluated as true in FF, so I ***-umed that it was evaluating true in IE as well. I removed the if statement, and it works.
Can anyone recommend a good javascript debugger for IE?
Thanks for your help, everyone.
Adam
-
The IE blog seems to recommend the Scripting Debugger, though personally I think it sucks ***.
If they only had a JavaScript console like Firefox...
-
Yeah, IE's messages regarding an 'error on the page' can be pretty cryptic and sometimes the line number is completely wrong. I can't believe they did almost absolutely no work with javascript for IE7.
-
They say JavaScript will improve in IE 8. Firefox will have JavaScript 2.0 support by then...
-
The scripting debugger's not too bad, most of the time it displays the code (unless it's extremely dynamic), you can step through, issue direct commands - alert(myvar) etc.