Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / July 2008



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
r_ahimsa_m@poczta.onet.pl - 03 Jul 2008 07:46 GMT
Hello,
I am learning JavaScript. I have a table on HTML page:

                <table id="announcement_fields" border="0">
                <tbody>
                <tr>
                <td><span class="obligatory">Offer type</span>:</td>
                <td>
                        <select name="offer_type"
onchange="ShowAnnouncementRows();">
                                <option value="" selected>(select)</option>
                                <option value="B">buy</option>
                                <option value="H">hire</option>
                                <option value="S">sell</option>
                                <option value="E">exchange</option>
                        </select>
                </td>
                </tr>
                ...
                <tr>
                <td><label for="caution">Caution:</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 ShowAnnouncementRows() at every change of
offer_type. Here is the code:

function ShowAnnouncementRows()
{
 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.getElementById("announcement_fields");
  var tbody = table.tBodies[0];
  if (announcementRows == null)
  {
   announcementRows = new Array();
    for (var r = 0; r < tbody.rows.length; r++) // remember table
      announcementRows.push(tbody.rows[r].innerHTML);
  }   
  for (var r = 1, len = tbody.rows.length; r < len; r++) // leave only first
row
 {
    tbody.removeChild(tbody.lastChild);
    var selIdx =
document.forms["announcement"].elements["offer_type"].selectedIndex;
    if (selIdx > -1)
      for (var r = 1; r < announcementRows.length; r++)
        if (visibility[selIdx][r] == 1) // show visible rows
        {
          var theRow = table.createElement("tr"); // HERE PROBLEM!!!
          theRow.innerHTML = announcementRows[r];
          tbody.appendChild(theRow);
        }
}

I have problems with debugging JavaScript but using alerts I found that the
code executes to the line:
        var theRow = table.createElement("tr");
and it doesn't continue. I don't know why and how to solve the problem. I
also tried document.createElement but it is not a solution.
Please help. Thanks in advance.
/RAM/
Martin Honnen - 03 Jul 2008 12:15 GMT
> I have problems with debugging JavaScript but using alerts I found that the
> code executes to the line:
>         var theRow = table.createElement("tr");
> and it doesn't continue. I don't know why and how to solve the problem. I
> also tried document.createElement but it is not a solution.

Well document.createElement is defined in the W3C DOM while
table.createElement (where table is a HTML table element object) is not
defined. So you will have to use document.createElement("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
http://msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

r_ahimsa_m@poczta.onet.pl - 04 Jul 2008 06:59 GMT
Thank you, you are god.
/RAM/
Thomas 'PointedEars' Lahn - 03 Jul 2008 12:33 GMT
> [...]
>   var table = document.getElementById("announcement_fields");
>   var tbody = table.tBodies[0];
>   if (announcementRows == null)

Should you not test `table' and `tbody' first?

>   {
>     [...]
>           var theRow = table.createElement("tr"); // HERE PROBLEM!!!
>           theRow.innerHTML = announcementRows[r];

You should avoid `innerHTML', especially with tables.

>           tbody.appendChild(theRow);
>         }
[quoted text clipped - 5 lines]
> and it doesn't continue. I don't know why and how to solve the problem. I
> also tried document.createElement but it is not a solution.

document.createElement("tr") should work, table.createElement("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
Signature

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

r_ahimsa_m@poczta.onet.pl - 04 Jul 2008 06:59 GMT
Thank you, you are god.
/RAM/
Evertjan. - 04 Jul 2008 08:09 GMT
wrote on 04 jul 2008 in comp.lang.javascript:

> Thank you, you are god.

You missed the second o.

Or was this triad posting on purpose?

Signature

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

RobG - 03 Jul 2008 12:52 GMT
On Jul 3, 6:39 pm, r_ahims...@poczta.onet.pl wrote:
> Hello,
> I am learning JavaScript. I have a table on HTML page:
[quoted text clipped - 6 lines]
>                         <select name="offer_type"
> onchange="ShowAnnouncementRows();">

By convention, function names starting with a capital letter are
reserved for constructors.

>                                 <option value="" selected>(select)</option>
>                                 <option value="B">buy</option>
[quoted text clipped - 8 lines]
>                 <td><label for="caution">Caution:</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>
>                 ...
[quoted text clipped - 16 lines]
>         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.getElementById("announcement_fields");
>   var tbody = table.tBodies[0];
[quoted text clipped - 3 lines]
>     for (var r = 0; r < tbody.rows.length; r++) // remember table
>       announcementRows.push(tbody.rows[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.length; r < len; r++) // leave only first
[quoted text clipped - 8 lines]
>         {
>           var theRow = table.createElement("tr"); // HERE PROBLEM!!!

The HTMLTableElement interface doesn't have a createElement method.

<UEL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#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.innerHTML = announcementRows[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:

 announcementRows.push(tbody.rows[r]);

then later

 tbody.appendChild(announcementRows[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.appendChild(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.createElement("tr");
> and it doesn't continue. I don't know why and how to solve the problem. I
> also tried document.createElement

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.createElement('tr');
 tbody.appendChild(newRow);

but that is unnecessary given the above.

--
Rob
r_ahimsa_m@poczta.onet.pl - 04 Jul 2008 06:59 GMT
Thank you, you are god.
/RAM/
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.