problem in line: var theRow = table.createElement("tr")

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r_ahimsa_m@poczta.onet.pl

    problem in line: var theRow = table.createElement("tr")

    Hello,
    I am learning JavaScript. I have a table on HTML page:

                    <table id="announcemen t_fields" border="0">
                    <tbody>
                    <tr>
                    <td><span class="obligato ry">Offer type</span>:</td>
                    <td>
                            <select name="offer_typ e"
    onchange="ShowA nnouncementRows ();">
                                    <option value="" selected>(selec t)</option>
                                    <option value="B">buy</option>
                                    <option value="H">hire</option>
                                    <option value="S">sell</option>
                                    <option value="E">excha nge</option>
                            </select>
                    </td>
                    </tr>
                    ...
                    <tr>
                    <td><label for="caution">C aution:</label></td>
                    <td><input type="checkbox" name="caution"> </td>
                    </tr>
                    ...
                    </tbody>
                    </table>

    I want table rows (with input controls) to be visible depending on the
    selection of offer_type. For example, I want input caution to be visible
    for offer_type = "H" or "(select)".
    As you see I am calling function ShowAnnouncemen tRows() at every change of
    offer_type. Here is the code:

    function ShowAnnouncemen tRows()
    {
    var visibility = new Array(     // can be more complicated
      new Array(1, 1, 1, 1, 1, 1, 1, 1),
            new Array(1, 1, 1, 1, 1, 0, 1, 1),
            new Array(1, 1, 1, 1, 1, 1, 1, 1),
            new Array(1, 1, 1, 1, 1, 0, 1, 1),
            new Array(1, 1, 1, 1, 1, 0, 1, 1)
      );
      var table = document.getEle mentById("annou ncement_fields" );
      var tbody = table.tBodies[0];
      if (announcementRo ws == null)
      {
    announcementRow s = new Array();
        for (var r = 0; r < tbody.rows.leng th; r++) // remember table
        announcementRow s.push(tbody.ro ws[r].innerHTML);
      }   
      for (var r = 1, len = tbody.rows.leng th; r < len; r++) // leave only first
    row
    {
      tbody.removeChi ld(tbody.lastCh ild);
        var selIdx =
    document.forms["announceme nt"].elements["offer_type "].selectedIndex;
        if (selIdx -1)
        for (var r = 1; r < announcementRow s.length; r++)
          if (visibility[selIdx][r] == 1) // show visible rows
            {
            var theRow = table.createEle ment("tr"); // HERE PROBLEM!!!
              theRow.innerHTM L = announcementRow s[r];
              tbody.appendChi ld(theRow);
            }
    }

    I have problems with debugging JavaScript but using alerts I found that the
    code executes to the line:
            var theRow = table.createEle ment("tr");
    and it doesn't continue. I don't know why and how to solve the problem. I
    also tried document.create Element but it is not a solution.
    Please help. Thanks in advance.
    /RAM/
  • Martin Honnen

    #2
    Re: problem in line: var theRow = table.createEle ment(&quot;tr&q uot;)

    r_ahimsa_m@pocz ta.onet.pl wrote:
    I have problems with debugging JavaScript but using alerts I found that the
    code executes to the line:
    var theRow = table.createEle ment("tr");
    and it doesn't continue. I don't know why and how to solve the problem. I
    also tried document.create Element but it is not a solution.
    Well document.create Element is defined in the W3C DOM while
    table.createEle ment (where table is a HTML table element object) is not
    defined. So you will have to use document.create Element("tr") if you
    want to create a new tr element.
    The remaining code also looks odd to me, there is no need to store the
    innerHTML of element nodes, you can simply store the element nodes
    themselves and reinsert them as needed.
    And setting innerHTML on tr elements does not work with IE anyway, see
    The innerHTML property of the Element interface gets or sets the HTML or XML markup contained within the element, omitting any shadow roots in both cases.



    --

    Martin Honnen

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: problem in line: var theRow = table.createEle ment(&quot;tr&q uot;)

      r_ahimsa_m@pocz ta.onet.pl wrote:
      [...]
      var table = document.getEle mentById("annou ncement_fields" );
      var tbody = table.tBodies[0];
      if (announcementRo ws == null)
      Should you not test `table' and `tbody' first?
      {
      [...]
      var theRow = table.createEle ment("tr"); // HERE PROBLEM!!!
      theRow.innerHTM L = announcementRow s[r];
      You should avoid `innerHTML', especially with tables.
      tbody.appendChi ld(theRow);
      }
      }
      >
      I have problems with debugging JavaScript but using alerts I found that the
      code executes to the line:
      var theRow = table.createEle ment("tr");
      and it doesn't continue. I don't know why and how to solve the problem. I
      also tried document.create Element but it is not a solution.
      document.create Element("tr") should work, table.createEle ment("tr") should
      not. However, in my tests in Firefox 3.0, when you assign `innerHTML'
      before you append the row, the resulting markup will not be functional.
      There are also specific DOM mutator methods for tables that you can try:

      <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-64060425>


      PointedEars
      --
      Anyone who slaps a 'this page is best viewed with Browser X' label on
      a Web page appears to be yearning for the bad old days, before the Web,
      when you had very little chance of reading a document written on another
      computer, another word processor, or another network. -- Tim Berners-Lee

      Comment

      • RobG

        #4
        Re: problem in line: var theRow = table.createEle ment(&quot;tr&q uot;)

        On Jul 3, 6:39 pm, r_ahims...@pocz ta.onet.pl wrote:
        Hello,
        I am learning JavaScript. I have a table on HTML page:
        >
                        <table id="announcemen t_fields" border="0">
                        <tbody>
                        <tr>
                        <td><span class="obligato ry">Offer type</span>:</td>
                        <td>
                                <select name="offer_typ e"
        onchange="ShowA nnouncementRows ();">
        By convention, function names starting with a capital letter are
        reserved for constructors.

                                        <option value="" selected>(selec t)</option>
                                        <option value="B">buy</option>
                                        <option value="H">hire</option>
                                        <option value="S">sell</option>
                                        <option value="E">excha nge</option>
                                </select>
                        </td>
                        </tr>
                        ...
                        <tr>
                        <td><label for="caution">C aution:</label></td>
                        <td><input type="checkbox" name="caution"> </td>
        The for attribute should contain the id of the related element, not
        the name.

        <URL: http://www.w3.org/TR/html4/interact/forms.html#adef-for >

                        </tr>
                        ...
                        </tbody>
                        </table>
        >
        I want table rows (with input controls) to be visible depending on the
        selection of offer_type. For example, I want input caution to be visible
        for offer_type = "H" or "(select)".
        As you see I am calling function ShowAnnouncemen tRows() at every change of
        offer_type. Here is the code:
        >
        function ShowAnnouncemen tRows()
        {
          var visibility = new Array(     // can be more complicated
                new Array(1, 1, 1, 1, 1, 1, 1, 1),
                new Array(1, 1, 1, 1, 1, 0, 1, 1),
                new Array(1, 1, 1, 1, 1, 1, 1, 1),
                new Array(1, 1, 1, 1, 1, 0, 1, 1),
                new Array(1, 1, 1, 1, 1, 0, 1, 1)
          );
        That might be easier to code as an array literal:

        var visibility = [
        [1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 0, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 0, 1, 1],
        [1, 1, 1, 1, 1, 0, 1, 1]
        ];

          var table = document.getEle mentById("annou ncement_fields" );
          var tbody = table.tBodies[0];
          if (announcementRo ws == null)
          {
            announcementRow s = new Array();
            for (var r = 0; r < tbody.rows.leng th; r++) // remember table
              announcementRow s.push(tbody.ro ws[r].innerHTML);
        Don't expect to be able to use innerHTML to create individual
        tableSection, row or cell elements - IE has problems with that. You
        can use innerHTML to create an entire table or the content of a cell.

          }   
          for (var r = 1, len = tbody.rows.leng th; r < len; r++) // leave only first
        row
          {
            tbody.removeChi ld(tbody.lastCh ild);
            var selIdx =
        document.forms["announceme nt"].elements["offer_type "].selectedIndex;
            if (selIdx -1)
              for (var r = 1; r < announcementRow s.length; r++)
                if (visibility[selIdx][r] == 1) // show visible rows
                {
                  var theRow = table.createEle ment("tr"); // HERE PROBLEM!!!
        The HTMLTableElemen t interface doesn't have a createElement method.

        <UEL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-64060425 >

        However, it does have an insertRow method, so:

        var theRow = table.insertRow (-1);


        will add a new row as the last row of the table and a reference to
        theRow (and you don't need to mess with the tbody).

                  theRow.innerHTM L = announcementRow s[r];
        Won't work in IE at least, it won't let you assign to the innerHTML
        property of table sections or rows.

        To make this strategy work, store a reference to the rows in your
        array then put back the one you want, so:

        announcementRow s.push(tbody.ro ws[r]);

        then later

        tbody.appendChi ld(announcement Rows[r]);


        But in any case, just set the style.display property of rows you want
        hidden to "none" and those you want visible to "" (empty string).
                  tbody.appendChi ld(theRow);
                }
        }
        >
        I have problems with debugging JavaScript
        Use Firefox with Firebug, learn IE's idiosyncrasies, check frequently
        as you code.

        but using alerts I found that the
        code executes to the line:
                var theRow = table.createEle ment("tr");
        and it doesn't continue. I don't know why and how to solve the problem. I
        also tried document.create Element
        You could use it instead of insertRow (in the browsers I've tested
        it's quite a bit faster if that matters):

        var newRow = document.create Element('tr');
        tbody.appendChi ld(newRow);

        but that is unnecessary given the above.


        --
        Rob

        Comment

        • r_ahimsa_m@poczta.onet.pl

          #5
          Re: problem in line: var theRow = table.createEle ment(&quot;tr&q uot;)

          Thank you, you are god.
          /RAM/

          Comment

          • r_ahimsa_m@poczta.onet.pl

            #6
            Re: problem in line: var theRow = table.createEle ment(&quot;tr&q uot;)

            Thank you, you are god.
            /RAM/

            Comment

            • r_ahimsa_m@poczta.onet.pl

              #7
              Re: problem in line: var theRow = table.createEle ment(&quot;tr&q uot;)

              Thank you, you are god.
              /RAM/

              Comment

              • Evertjan.

                #8
                Re: problem in line: var theRow = table.createEle ment(&quot;tr&q uot;)

                wrote on 04 jul 2008 in comp.lang.javas cript:
                Thank you, you are god.
                You missed the second o.

                Or was this triad posting on purpose?

                --
                Evertjan.
                The Netherlands.
                (Please change the x'es to dots in my emailaddress)

                Comment

                Working...