Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing dynamically added row field value

    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

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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.

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    Firstly thanks for your reply.

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

    Thanks
    Dasani
    Code:
    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);
      }
    Last edited by Kor; 08-29-2007 at 02:48 PM. Reason: wr

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    try:
    Code:
    for (var i =1; i<rownum+1 ; i++) {
    var num = document['inquiry']['tl'+i].value;
    ...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The same error occurs. It works fine till tl4, but gives error at tl5.

    Thanks

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    try setting the name attribute directly, like:
    Code:
    S1.name="tl" + rownum;
    instead of
    Code:
    S1.setAttribute("name","tl" + rownum);

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no luck. same result.

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can somebody help me in this please, i am stuck with it.

    Thanks

  • #9
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    This seems to work:
    Code:
    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!

  • #10
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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
    Code:
    function addRow(id){
    ...
       document.inquiry['tl' + rownum] = S1;
       document.inquiry['df' + rownum] = S2;
       document.inquiry['efdt' + rownum] = S3;
       document.inquiry['edt' + rownum] = S4;
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #11
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Thank you all for your reply's, especially MCJWB whose code is working great.
    I really appreciate all your help.

    Regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •