...

View Full Version : only 1 row visible



mglowacki
05-26-2006, 09:33 AM
Hi, I've got this code fragment for adding new table into outer table. The problem is that table should contain 2 rows, while only one is displayed. When i change var name of second table from tr1 to tr, the second one is displayed, the first not. When I leave like below first one is visble, second not. Can anyone take a quick look?

table = document.createElement("table");
tbody = document.createElement("tbody");
tr = document.createElement("tr");
td = document.createElement("td");
font = document.createElement("font");
font.className = "bet-name";
font.appendChild(document.createTextNode(betName));
td.align = "left";
td.appendChild(font);
tr.appendChild(td);
td = document.createElement("td");
td.onclick = function(){removeBetTable(this)};
img = document.createElement("img");
img.src = "./template/images/betslip/x.gif";
td.appendChild(img);
tr.appendChild(td);
tbody.appendChild(tr);

tr1 = document.createElement("tr");
td = document.createElement("td");
td.align = "left";
td.setAttribute("style","padding-left: 2px");
font = document.createElement("font");
font.className = "bet-name";
font.appendChild(document.createTextNode("Outcome: " + outcomeName));
td.appendChild(font);
tr1.appendChild(td);

td = document.createElement("td");
td.align = "right";
td.setAttribute("style","padding-right: 2px");
font = document.createElement("font");
font.className = "bet-name";
font.appendChild(document.createTextNode("Odd: " + outcomeOdd));
td.appendChild(font);

hidden = document.createElement("input");
hidden.type = "hidden";
hidden.id = "odd" + outcomeId;
hidden.value = outcomeOdd;
td.appendChild(hidden);

tr1.appendChild(td);


tbody.appendChild(tr1);
table.appendChild(tbody);

mglowacki
05-26-2006, 03:27 PM
ok, some more data. The problem should be here, as generated table node is not inserted into hierarchy (his parent should be td of tr of tbody and isn't):

function addBet(betName, outcomeName, outcomeOdd, outcomeId) {
table = document.getElementById("parentTable");
if(table.getElementsByTagName('tbody').length) {
tbody = table.getElementsByTagName('tbody')[0];
}

tr = document.createElement("tr");
td = document.createElement("td");
gameTable = createBetTable(betName,outcomeName, outcomeOdd,outcomeId);
td.appendChild(gameTable);
tr.appendChild(td);
tbody.appendChild(tr);
}

that gives error:

Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" location: "http://localhost:8080/xxx/script.js Line: 12"]

Line 12 is td.appendChild(gameTable); - I see nothing wrong in adding table to td node. Am I wrong. Changing code to:

td.appendChild(createBetTable(betName,outcomeName, outcomeOdd,outcomeId));

inserts table but as I mentioned not into proper place in hierarchy.

any thoughts?

Kor
05-26-2006, 04:04 PM
Unless is absolutely necessary, use local variables, not global variables

function myFunction(){
var myvar1 //this is a local variable
myvar2//this is a global variable
}

The problem is that you have the variable td as global. After you have returned from the function createBetTable() the variable was changed by that function, as you have a td there as well, so that now td is another object.

Use local variables
var td
var tr
.........

mglowacki
05-26-2006, 04:47 PM
thanks, that solved all my problems.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum