SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Member
    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">
    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

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    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.

  3. #3
    SitePoint Member
    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

  4. #4
    SitePoint Guru Ize's Avatar
    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.

  5. #5
    SitePoint Member
    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

  6. #6
    SitePoint Wizard
    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?

  7. #7
    SitePoint Member
    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>

  8. #8
    SitePoint Member
    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

  9. #9
    SitePoint Wizard
    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)

  10. #10
    SitePoint Member
    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

  11. #11
    SitePoint Wizard Pepejeria's Avatar
    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...

  12. #12
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    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.

  13. #13
    SitePoint Wizard Pepejeria's Avatar
    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...

  14. #14
    SitePoint Wizard
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •