Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    createElement IE issue

    I have a problem that I have been told to fix. The code that is there work perfectly in Firefox, but throws errors in IE and will not work.

    Code:
    var newtbody = document.createElement("tbody");
    newtbody.id = "ROW"+newkey;
    document.getElementById(trElement).parentNode.insertBefore(newtbody,document.getElementById(trElement));
    newtbody.innerHTML = gcp;
    gcp has the HTML for inside the tbody tag. The error I get in IE is the "Unknown runtime error". I have tried some of the "solutions" that I have seen online but none have worked. Can anyone help?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Ummm...you already *HAVE* a <tbody> (automatically generated) if your table has a <tr> row in it [at least with IE].

    Assuming your document.getElementById(trElement) is indeed getting a reference to a <tr>, then it's no wonder the code barfs. The parentNode of a <tr> *IS* the <tbody> [again, at least in IE], so you are trying to insert a <tbody> *INTO* the existing <tbody>!!!

    *possibly* the way to solve this is to check to see if the tbody already exists and, if so, use it instead of trying to create a new one.

    Can you start from the <table>, instead of from the existing <tr> row??
    Code:
    var tbl = document.getElementById(idOfTheTable);
    var tbody = tbl.getElementsByTagName("tbody");
    if ( tbody == null )
    {
        var firstRow = tbl.getElementsByTagName("tr")[0];
        var tbody = document.createElement("tbody");
        tbl.insertBefore( tbody, firstRow );
    }
    tbody.innerHTML = gcp;
    ???

    UTTERLY untested. Just kind of thinking out loud.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Yeah, in MSIE *and* FF, you automatically get a <tbody> when you create a <table>.

    And, as I said, the parentNode of any <tr> is the <tbody>, not the <table>.

    So I dunno why your code works in FF, either. Doesn't seem reasonable to insert a tbody into a tbody.

    Test code:
    Code:
    <html>
    <head>
    <script>
    function show( )
    {
        var tbl = document.getElementById("TheTable");
        var tbs = tbl.getElementsByTagName("tbody");
        if ( tbs != null && tbs.length > 0 )
        {
            alert( "TheTable has a <tbody> with contents:\n" + tbs[0].innerHTML);
        } else {
            alert( "TheTable does not have a <tbody>");
        }
        var tr = document.getElementById("row2");
        alert( "The parentNode of row2 is a " + tr.parentNode.tagName 
               + " that contains:\n" + tr.parentNode.innerHTML);
    }
    </script>
    </head>
    <body onload="show()">
    <table id="TheTable" border=1 cellpadding=10>
    <tr>
        <td>cell 1</td><td>cell 2</td>
    </tr>
    <tr id="row2">
        <td>cell 1</td><td>cell 2</td>
    </tr>
    </table>
    </body>
    </html>
    Try it in both FF and MSIE. And any other browser.
    Last edited by Old Pedant; 06-19-2010 at 01:05 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    The code for all of this is very bad. My boss if the one who wrote it and now I have to fix it. The trElement is not a tr tag as one might think. It is a tbody tag. Each row is encased in a tbody tag. The new tbody is being created and added in IE. The new html is just not being attributed to it. That is where my problem lies.
    Last edited by chiefbutz; 06-21-2010 at 04:37 PM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,559 Times in 4,523 Posts
    Well, if the innerHTML you are trying to add to the tbody includes <TR> or <TD> elements, then you can't do it that way. MSIE insists that you add them to <TBODY>s using DOM methods, not by just writing HTML.

    You might want to read this:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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