chrisfozz
01-06-2012, 05:27 PM
This works, to a degree.
“Click Here” and the page dynamically adds a text box with the default value 100.
Users can the manually change the value if they wish.
My problem is with the getArray function, at present it works only when 3 text boxes are added; 1 or 2 produces no result and more than 3 only gives a result for the first three. I need to add the content of each text box to the tpArray whatever the number.
I’ve tried various ways to loop through this with no success; any help would be greatly appreciated.
<html>
<head>
<script type = "text/javascript">
var myVar = 0;
var num
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = "Label "+myVar+" <input type='text' size = '10' name='txtbox"+num+"' onblur = 'capture(this)' value = '"+100+"' />"
ni.appendChild(newdiv);
myVar ++;
}
var tpArray=new Array();
function getArray(){
tpArray = [document.form1.txtbox1.value, document.form1.txtbox2.value, document.form1.txtbox3.value];
}
function printArray(){
for(var i=0;i<tpArray.length;i++){
document.write("tpArray["+i+"] is "+tpArray[i]+"<br>");
}
}
</script>
</head>
<body>
<form name="form1" action="" method="get">
<input type="hidden" value="0" id="theValue" />
<div id="myDiv"> </div>
</form>
<div id = 'clicker' onClick="addElement()"><h1>click here to add an element</h1></div>
<button type="button" onclick="getArray()">getArray fuction</button>
<button type="button" onclick="printArray()">printArray function</button>
</body>
</html>
“Click Here” and the page dynamically adds a text box with the default value 100.
Users can the manually change the value if they wish.
My problem is with the getArray function, at present it works only when 3 text boxes are added; 1 or 2 produces no result and more than 3 only gives a result for the first three. I need to add the content of each text box to the tpArray whatever the number.
I’ve tried various ways to loop through this with no success; any help would be greatly appreciated.
<html>
<head>
<script type = "text/javascript">
var myVar = 0;
var num
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = "Label "+myVar+" <input type='text' size = '10' name='txtbox"+num+"' onblur = 'capture(this)' value = '"+100+"' />"
ni.appendChild(newdiv);
myVar ++;
}
var tpArray=new Array();
function getArray(){
tpArray = [document.form1.txtbox1.value, document.form1.txtbox2.value, document.form1.txtbox3.value];
}
function printArray(){
for(var i=0;i<tpArray.length;i++){
document.write("tpArray["+i+"] is "+tpArray[i]+"<br>");
}
}
</script>
</head>
<body>
<form name="form1" action="" method="get">
<input type="hidden" value="0" id="theValue" />
<div id="myDiv"> </div>
</form>
<div id = 'clicker' onClick="addElement()"><h1>click here to add an element</h1></div>
<button type="button" onclick="getArray()">getArray fuction</button>
<button type="button" onclick="printArray()">printArray function</button>
</body>
</html>