Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get value from dynamically created textbox in html. but I can't.. plez help.

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Matrix for Life</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<style type="text/css">
		#newtable{
			border-left:2px solid #999;
			border-right:2px solid #999;
			font-family:verdana,arial,helvetica,sans-serif;
			font-size:18px;
			margin:auto;
		}
		input.node{
			hight: 45px;
			width: 55px;
			border:1px doted #999;
		}
		MatA{
			
		}
		</style>
		<script type="text/javascript">
		
		function AddMatrix() {
			row=new Array();
			cell=new Array();
			row_num=document.getElementById("noofrows").value;
			cell_num=document.getElementById("noofcols").value;
			tab=document.createElement('table');
			tab.setAttribute('id','newtable');
			tbo=document.createElement('tbody');
			for(c=0;c<row_num;c++){
				row[c]=document.createElement('tr');
				for(k=0;k<cell_num;k++) {
					cell[k]=document.createElement('td');
					var input = document.createElement("input"); 
					input.type = "text";
					input.name = 'node'+c+','+k;
					input.setAttribute('class','node');
					cell[k].appendChild(input);
					row[c].appendChild(cell[k]);
				}
				tbo.appendChild(row[c]);
			}
			tab.appendChild(tbo);
			document.getElementById('mytable').appendChild(tab);
		}
			
		function show(){
			var val = document.getElementById("mytable").rows[0].cells[0].getElementsByTagName("input").value; 
			alert(val);
		}
		</script>
	</head>
	<body>
		<div>
			<h3>Matrix</h3>
			Number of Rows: <input type="text" id="noofrows" value='2' />
			Number of Colomns: <input type="text" id="noofcols" value='3'/>
			<input type="button" id="add" value="Add" onclick="AddMatrix()"/>
		</div>
		<div id="mytable"></div>
		<input type="button" id="show" value="Show" onclick="show()"/>
	</body>
</html>
Posted

I think you should provide the ids)property) to the textboxes and that's too in pattern.

Like if you have three textboxes initialy with ids tb1 , tb2,tb3 respectively.

Then fill the id attribute of dynamicaly created textbox with tb4(where 4 is from some counter

value or you may get it from getElementsByTagName("input").length).

Finaly,

through getElementById("tb"+counter).value=someValue you can insert value to the dynamicaly created textbox.

Best Of Luck..
 
Share this answer
 
v2
try this code.
XML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Matrix for Life</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <style type="text/css">
        #newtable{
            border-left:2px solid #999;
            border-right:2px solid #999;
            font-family:verdana,arial,helvetica,sans-serif;
            font-size:18px;
            margin:auto;
        }
        input.node{
            hight: 45px;
            width: 55px;
            border:1px doted #999;
        }
        MatA{

        }
        </style>
        <script type="text/javascript">

        function AddMatrix() {
            row=new Array();
            cell=new Array();
            row_num=document.getElementById("noofrows").value;
            cell_num=document.getElementById("noofcols").value;
            tab=document.createElement('table');
            tab.setAttribute('id','newtable');
            tbo=document.createElement('tbody');
            for(c=0;c<row_num;c++){
                row[c]=document.createElement('tr');
                row[c].TagName="tr";
                for(k=0;k<cell_num;k++) {
                    cell[k]=document.createElement('td');
                    cell[k].TagName="td";
                    var input = document.createElement("input");
                    input.type = "text";
                    input.name = 'node'+c+','+k;
                    input.setAttribute('class','node');
                    cell[k].appendChild(input);
                    row[c].appendChild(cell[k]);
                }
                tbo.appendChild(row[c]);
            }
            tab.appendChild(tbo);
            document.getElementById('mytable').appendChild(tab);
        }
        </script>
    <script type="text/javascript">
        function show1()
        {
         var tblData = new Array();
        var table = document.getElementById("newtable");
        var i;
        var txt;
        var txt2;
        tr = table.getElementsByTagName('tr');
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName('td');
            var td2 = td[1].childNodes[0].value;
            var td1 = td[0].childNodes[0].value;
           var td3 = td[2].childNodes[0].value;
            var val = td1+td2+td3;
            alert(val);

            }
        }
        </script>
    </head>
    <body>
        <div>
            <h3>Matrix</h3>
            Number of Rows: <input type="text" id="noofrows" value='2' />
            Number of Colomns: <input type="text" id="noofcols" value='3'/>
            <input type="button" id="add" value="Add" onclick="AddMatrix()"/>
        </div>
        <div id="mytable"></div>
        <input type="button" id="show" value="Show" onclick="show1()"/>
    </body>
</html>
 
Share this answer
 
v2
Comments
Member 10703757 17-Sep-14 6:04am    
i want to create a pop up in which a form will show,and the field is name,address,city and image,and the image must store in database and i like to edit and update inside the pop up window,will anyone help me regarding tha??????
i want to create a pop up in which a form will show,and the field is name,address,city and image,and the image must store in database and i like to edit and update inside the pop up window,will anyone help me regarding tha??????
 
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