...

View Full Version : Accessing dynamically added row field value



dasani
08-28-2007, 10:11 PM
Hi,
There are 4 physical rows and from 5th onwards it is dynamic rows.
The below function works fine until rownum =4, but for the 5th row there is an error at the second alert stating "null or not an object"

How can i access the values from dynamic rows ? Please suggest

function Check() {

for (var i =1; i<rownum+1 ; i++) {
var tl = "tl"+i;
alert("tl :"+tl);
alert(document.inquiry[tl].value);
var num = document.inquiry[tl].value;
if (checkWhole(num) ){.....

Thanks & Regards,
Dasani

mcjwb
08-29-2007, 09:13 AM
How are you adding the rows to the table, with innerHTML or DOM methods?

You need to use DOM methods if you then want to access the new rows.

dasani
08-29-2007, 02:26 PM
Hi,
Firstly thanks for your reply.

I am using the following code to create the dynamic rows.

Thanks
Dasani


var rownum = 4;

function addRow(id){
var tbody = document.getElementById
(id).getElementsByTagName("TBODY")[0];
rownum++;
var row = document.createElement("TR")
var td1 = document.createElement("TD")
var td2 = document.createElement("TD")
var td3 = document.createElement("TD")
var td4 = document.createElement("TD")

S1 = document.createElement("input");
S1.setAttribute("type","text");
S1.setAttribute("name","tl" + rownum);
S1.setAttribute("maxLength","11");
td1.setAttribute("align","center");
S2 = document.createElement("input");
S2.setAttribute("type","text");
S2.setAttribute("name","df" + rownum);
S2.setAttribute("maxLength","10");
td2.setAttribute("align","center");
S3 = document.createElement("input");
S3.setAttribute("type","text");
S3.setAttribute("name","efdt" + rownum);
S3.setAttribute("maxLength","10");
var dt = document.inquiry.dt.value ;
S3.setAttribute("value",dt);
td3.setAttribute("align","center");


S4 = document.createElement("input");
S4.setAttribute("type","text");
S4.setAttribute("name","edt" + rownum);
S4.setAttribute("value","12/31/9999");
S4.setAttribute("maxLength","10");
td4.setAttribute("align","center");
td1.appendChild(S1);
td2.appendChild(S2);
td3.appendChild(S3);
td4.appendChild(S4);

row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
row.appendChild(td4);

tbody.appendChild(row);
}

Kor
08-29-2007, 02:55 PM
try:


for (var i =1; i<rownum+1 ; i++) {
var num = document['inquiry']['tl'+i].value;
...

dasani
08-29-2007, 04:00 PM
The same error occurs. It works fine till tl4, but gives error at tl5.

Thanks

mcjwb
08-29-2007, 04:10 PM
try setting the name attribute directly, like:

S1.name="tl" + rownum;
instead of

S1.setAttribute("name","tl" + rownum);

dasani
08-29-2007, 04:27 PM
no luck. same result.

dasani
08-30-2007, 02:14 PM
Can somebody help me in this please, i am stuck with it.

Thanks

mcjwb
08-30-2007, 03:18 PM
This seems to work:

var rownum = 4;
function addRow(id){
var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
rownum++;
var row = document.createElement("TR")
tbody.appendChild(row);
var td1 = document.createElement("TD")
row.appendChild(td1);
var td2 = document.createElement("TD")
row.appendChild(td2);
var td3 = document.createElement("TD")
row.appendChild(td3);
var td4 = document.createElement("TD")
row.appendChild(td4);
var S1 = document.createElement("<input type='text' name='tl"+rownum+"'>");
S1.setAttribute("maxLength","11");
td1.setAttribute("align","center");
td1.appendChild(S1);
var S2 = document.createElement("<input type='text' name='df"+rownum+"'>");
S2.setAttribute("maxLength","10");
td2.setAttribute("align","center");
td2.appendChild(S2);
var S3 = document.createElement("<input type='text' name='efdt"+rownum+"'>");
S3.setAttribute("maxLength","10");
S3.setAttribute("value",document.inquiry.dt.value);
td3.setAttribute("align","center");
td3.appendChild(S3);
var S4 = document.createElement("<input type='text' name='edt"+rownum+"'>");
S4.setAttribute("value","12/31/9999");
S4.setAttribute("maxLength","10");
td4.setAttribute("align","center");
td4.appendChild(S4);
}

I have no idea why setAttribute("name",... or S1.name=... don't work. Perhaps it is a bug in Javascript, it certainly wouldn't be the first!

shyam
08-30-2007, 03:28 PM
the best way to solve ur problem is to add ids to ur form elements which are equivalent to the name attributes and use the document.getElementById

but, sometimes i've found it convenient to add elements to the form collection explicitly especially when IE is so stubborn about adding the form elements to the form object

function addRow(id){
...
document.inquiry['tl' + rownum] = S1;
document.inquiry['df' + rownum] = S2;
document.inquiry['efdt' + rownum] = S3;
document.inquiry['edt' + rownum] = S4;
}

dasani
08-30-2007, 04:49 PM
Thank you all for your reply's, especially MCJWB whose code is working great.
I really appreciate all your help.

Regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum