SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Thread: removeAttribute in IE
-
Mar 20, 2007, 11:35 #1
- Join Date
- Nov 2005
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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">
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 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
-
Mar 20, 2007, 12:10 #2
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I think you're probably better off with table2.width = ''. IE doesn't support removeAttribute very well.
-
Mar 20, 2007, 12:18 #3
- Join Date
- Nov 2005
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Mar 20, 2007, 12:36 #4
- Join Date
- Nov 2005
- Location
- The Netherlands
- Posts
- 808
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Mar 20, 2007, 12:39 #5
- Join Date
- Nov 2005
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Ize,
I appreciate your reply. I tried uppercase with no luck, still.
Thanks,
Adam
-
Mar 20, 2007, 12:51 #6
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Does it throw an error in IE or do you not get the expected results?
-
Mar 20, 2007, 12:58 #7
- Join Date
- Nov 2005
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Jim,
No error. I just don't get the expected results in IE. Nothing happens.
Thanks,
Adam
-
Mar 20, 2007, 13:07 #8
- Join Date
- Nov 2005
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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>
-
Mar 20, 2007, 13:08 #9
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Kind of amusing.. this works in IE6:
table2.removeAttribute("width");
(lowercase)
-
Mar 20, 2007, 13:21 #10
- Join Date
- Nov 2005
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
.......
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
-
Mar 20, 2007, 14:30 #11
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The IE blog seems to recommend the Scripting Debugger, though personally I think it sucks ***.
If they only had a JavaScript console like Firefox...
-
Mar 20, 2007, 14:35 #12
- Join Date
- Sep 2005
- Location
- Tanzania
- Posts
- 4,662
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Mar 20, 2007, 14:51 #13
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
They say JavaScript will improve in IE 8. Firefox will have JavaScript 2.0 support by then...
-
Mar 20, 2007, 14:55 #14
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
Bookmarks