javascript insertRow doesn't work ;-(

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • milkyway

    javascript insertRow doesn't work ;-(

    Hello,

    I am working with an old Java Script code and have found the following
    but after putting in alerts I have found that insertRow doesn't work.

    What is the problem?

    TIA

    function addRowDOM (tableID) {
    // pass every cell content as a futher arg
    alert("here");
    var table =
    document.all ? document.all[tableID] :
    document.getEle mentById(tableI D);
    alert("here 2");
    if (arguments.leng th > 1) {
    alert(table.row s.length);
    var row = table.insertRow (table.rows.len gth);
    alert("after var");
    if (document.all) {
    alert("second if");
    for (var i = 1; i < arguments.lengt h; i++) {
    alert("in the for loop");
    var cell = row.insertCell( i - 1);
    cell.innerHTML = arguments[i];
    }
    }
    }

  • RobG

    #2
    Re: javascript insertRow doesn't work ;-(

    milkyway wrote:[color=blue]
    > Hello,
    >
    > I am working with an old Java Script code and have found the following
    > but after putting in alerts I have found that insertRow doesn't work.
    >
    > What is the problem?[/color]

    You were missing a single closing brace.

    Others may wish to pitch in, but it seems to me that insertRow and
    insertCell are not part of the W3C DOM interface. As far as I can
    tell, they are a legacy of Netscape (3? 4?) that is also implemented by
    IE and sometimes referred to as the Table Object Model (TOM).

    There is only one interface: the DOM. Whilst Mozilla et al still
    support some of the older methods (sometimes called "specificat ion
    level 0") you should use those of the applicable standard.

    The W3C reference is here:


    <URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1590626201>

    or for Firefox/Mozilla in a simplified form that includes legacy stuff,
    try here:

    <URL:http://www.mozilla.org/docs/dom/domref/>

    As an example, I've included a modified version of your code below. It
    does minimal feature checking and works in the 'zillas and IE 6.

    Note that if you don't include the <tbody> element and append your rows
    to the <table>, then Firefox appends them to the table, not the tbody
    element. If no tbody is in the HTML, Firefox will include it in its
    DOM at runtime, but the rows are still added to the table, not the
    tbody. Whilst this may be strictly correct, it is a bit counter
    intuitive. Therefore it's probably best to include a tbody element in
    the HTML and append to that to prevent irregularities. A table can
    have multiple tbody elements.

    insertRow seems to always add to the tBody, whether one is in the HTML
    or not.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head><title>In sert Row</title>
    <script type="text/javascript">
    function addRowDOM0(tabl eID) {
    // pass every cell content as a futher arg
    var table = document.all ? document.all[tableID] :
    document.getEle mentById(tableI D);
    if (arguments.leng th > 1) {
    var row = table.insertRow (table.rows.len gth);
    for (var i = 1; i < arguments.lengt h; i++) {
    var cell = row.insertCell( i - 1);
    cell.innerHTML = arguments[i];
    }
    }
    }

    function addRowDOM1(tRef ){
    var oTable = document.getEle mentById(tRef);
    if (oTable && document.create Element) {
    var oRow = document.create Element('TR');
    for (var i=1; i<arguments.len gth; i++) {
    var oCell = document.create Element('TD');
    var oTxt = document.create TextNode(argume nts[i]);
    oCell.appendChi ld(oTxt);
    oRow.appendChil d(oCell);
    }
    oTable.appendCh ild(oRow);
    }
    }
    </script>
    </head>
    <body>
    <table><tbody id="fred">
    <tr>
    <td onclick="
    addRowDOM0('fre d','old way 1','old way 2');
    ">do old way</td>
    <td onclick="
    addRowDOM1('fre d','new way 1','new way 2');
    ">New way</td>
    </tr>
    </tbody></table>
    </body>
    </html>

    --
    Rob

    Comment

    • Michael Winter

      #3
      Re: javascript insertRow doesn't work ;-(

      On Tue, 07 Dec 2004 23:57:34 GMT, RobG <rgqld@iinet.ne t.auau> wrote:

      [snip]
      [color=blue]
      > The W3C reference is here:
      >
      > <URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1590626201>[/color]

      That's *one* of the specifications. Unlike DOM 1, DOM 2 (and all
      subsequent versions) is split into modules, one of which is DOM 2 HTML. In
      there you will find objects and methods specific to HTML documents,
      including those for the manipulation of tables. See
      <URL:http://www.w3.org/DOM/DOMTR.html#DOML 2>.

      By the way, I believe it best to refer to the undated URLs unless you're
      referencing a specific document. So, for example, instead of

      <URL:http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109>

      use

      <URL:http://www.w3.org/TR/DOM-Level-2-HTML>

      This latter form is listed as the "Latest version" at the beginning of
      most (all?) W3C technical reports.

      [snip]
      [color=blue]
      > insertRow seems to always add to the tBody, whether one is in the
      > HTML or not.[/color]

      By design. See
      <URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903>.

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • RobG

        #4
        Re: javascript insertRow doesn't work ;-(

        Michael Winter wrote:
        [...][color=blue]
        >
        > <URL:http://www.w3.org/TR/DOM-Level-2-HTML>
        >
        > This latter form is listed as the "Latest version" at the beginning of
        > most (all?) W3C technical reports.
        >
        > [snip]
        >[color=green]
        >> insertRow seems to always add to the tBody, whether one is in the
        >> HTML or not.[/color]
        >
        >
        > By design. See
        > <URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903>.
        >[/color]

        Thanks Mike. I find navigating around the W3C site painful, but you've
        given me the impetus to tidy my bookmarks once and for all.

        --
        Rob

        Comment

        Working...