Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am trying to add a new row dynamically to a html table using a javascript function.

I am trying to populate the value for each text in each row and while doing so,I am able to add the data which has single words but if I have something like Last Name, Middle Name only the first word is being displayed ...
Can anyone suggest me what I need to modify in the function to get the whole string separated by white spaces?
Please check the below code snippet .
JavaScript
function addRow(tbl_name) {
           var tbody = document.getElementById(tbl_name).getElementsByTagName("TBODY")[0];
            // create row
            var row = document.createElement("TR");
            // create table cell 1
            var td1 = document.createElement("TD")
//            var abc=GetInnerText("lsTest");
var abc="Last Name";
            var strHtml1 = "<center><input id="\"Text1\"" type="\"text\"" name=" + getName + " size="\"5\"" value=" + abc + " style="\"background-color:#E2E6C8;margin:0;\"" readonly=""> </input></center>";
            td1.innerHTML = strHtml1.replace(/!count!/g, count);
            // create table cell 2
            var td2 = document.createElement("TD")          
            var getValue = GetInnerText("Select1");
            var getName = "txtBox" + count;
            var strHtml2 = "<center><input id="\"Text1\"" type="\"text\"" name=" + getName + " size="\"5\"" value=" + getValue + " style="\"background-color:#E2E6C8;margin:0;\"" readonly=""> </input></center>";  
            td2.innerHTML = strHtml2.replace(/!count!/g, count);
            // create table cell 3
            var td3 = document.createElement("TD")
            var strHtml3 = "<input type="\"text\"" name="\"in_name\"" value="\"agagag\"" size="\"5\"" maxlength="\"5\"" style="\"height:24;border:" 1="" mode="hold" />            td3.innerHTML = strHtml3.replace(/!count!/g, count);
            // append data to row
            row.appendChild(td1);
            row.appendChild(td2);
            row.appendChild(td3);
            // add to count variable
            count = parseInt(count) + 1;
            // append row to table
            tbody.appendChild(row);
        }

Thanks!
Posted
Updated 25-May-10 4:46am
v3

1 solution

You are missing quotes in the value(value=Last Name).Thats why only first word is getting recognized.It should be value="Last Name"

Change the markup as below

value=\"" + abc + "\"

This will work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900