By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/
Jul 3 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
r_********@poczta.onet.pl wrote:
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/libr...97(VS.85).aspx
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 3 '08 #2

P: n/a
r_********@poczta.onet.pl wrote:
[...]
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);
}
}

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.
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
--
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
Jul 3 '08 #3

P: n/a
On Jul 3, 6:39*pm, r_ahims...@poczta.onet.pl wrote:
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();">
By convention, function names starting with a capital letter are
reserved for constructors.

* * * * * * * * * * * * * * * * <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>
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 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)
* );
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];
* if (announcementRows == null)
* {
* * announcementRows = new Array();
* * 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
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!!!
The HTMLTableElement 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.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
Jul 3 '08 #4

P: n/a
Thank you, you are god.
/RAM/
Jul 4 '08 #5

P: n/a
Thank you, you are god.
/RAM/
Jul 4 '08 #6

P: n/a
Thank you, you are god.
/RAM/
Jul 4 '08 #7

P: n/a
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?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 4 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.