473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
7 1814
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
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
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
Thank you, you are god.
/RAM/
Jul 4 '08 #5
Thank you, you are god.
/RAM/
Jul 4 '08 #6
Thank you, you are god.
/RAM/
Jul 4 '08 #7
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Bradbury | last post by:
I currently have an iframe on a webpage into which users can insert content. They can further customise the text as I've included buttons such as Bold, Italic, Bullet point etc. This is done along...
1
by: avihaimar | last post by:
hello i have the code below: <html> <body> <table> <div id="div1" style="display :none"> <tr> <td> uuu </td>
2
by: bissatch | last post by:
Hi, I am trying to use JavaScript to write a table column on a web page. The code is as follows: <html> <head> <script> function displaycount() {
4
by: millw0rm | last post by:
Hi wats wrng with this code??? it works fine on IE6 but not on FireFox 1.5??? var anOption = document.createElement("OPTION"); document.getElementById("category").options.add(anOption);...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
0
by: Michael | last post by:
Hi. I am building asp page. One of the <tdcoomponents should include text field from database which is include whole HTML page taken once from another web site. And ofcause this text consist of...
2
ramprabu
by: ramprabu | last post by:
Hello, I will give the sample code of html. Here first table only apply border 1 width. other tables are border 0. The problem is border=0 means border was not visible but it takes white border...
7
by: petr.jakes.tpc | last post by:
Hi group, I would like to convert the output of the SQL query, or more generally I would like to convert any "table" data to the html table. I would like to set some rules to format cells,...
1
by: aloksingh83 | last post by:
This error coming when i am using telerik grid.It works fine in asp.net grid what i am trying to do is taking values from textbox and show in grid view... here is the code: <script...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.