...

View Full Version : inserting the value of a text box that's inside a table into an array?



monkeycoder
12-12-2007, 08:32 PM
hi there,

i Need a bit of a hand with "inserting the value of a text box that's inside a table into an array" so what I'm looking for is some code that will take whats in the text box then make a place in the array and store it there. the array must have no set size. wondering is it something to do with push or pop?

rnd me
12-13-2007, 09:15 AM
hi there,

i Need a bit of a hand with "inserting the value of a text box that's inside a table into an array" so what I'm looking for is some code that will take whats in the text box then make a place in the array and store it there. the array must have no set size. wondering is it something to do with push or pop?



this should do.
the array will be set by the table.
you might have to steer the code a bit for your project, but this should get you started...




<table>
<tr> <td><input type='text' /></td>
<td><input type='text' /></td>
<td><input type='text' /></td>
<td><input type='text' /></td>
</tr><tr>
<td><input type='text' /></td>
<td><input type='text' /></td>
<td><input type='text' /></td>
<td><input type='text' /></td>
</tr><tr>
<td><input type='text' /></td>
<td><input type='text' /></td>
<td><input type='text' /></td>
<td><input type='text' /></td>
</tr></table>

<br />

<textarea id="ta1" rows="15" cols="60" ></textarea>

<script>

var ray=[[]]; //the array to store table changes

// some support code
function el(tid) { return document.getElementById(tid);}
function tags(elm){ var o=[], i=0, r=document.getElementsByTagName(elm), mx=r.length; for(var z=0;z<mx ;z++) {o[z]=r[z]} ; return o; }
function getParent(elm, tagname) { tops:for (var z = 0; z < 99; z++) {var t = elm.parentNode ? elm.parentNode : elm.parentElement ? elm.parentElement : elm;if (!t.tagName) {return elm;}if (!tagname) {return t;}if (tagname==(tagname * -1 )) {if (z == tagname) {return t;}elm = t;continue tops;}if (t.tagName.toLowerCase().match(tagname.toLowerCase())) {return t;}elm = t;}}



//custom code below
for(var z=0, mx=tags("tr").length; z<mx;z++){ ray[z]=[]; } //init array by length of table row (writing a col to an undefined row cause error!)

for(var z=0,tg=tags("input"), mx=tg.length; z<mx;z++){ //updrage all the text inputs to modify the array
var t=tg[z];
t.value=0;
t.size=4;
t.setAttribute("onchange","doRay(this)");
}//next text input

function doRay(t){ //modify the array with the value of the cell
var col=getParent(t, "td").cellIndex ||0;
var row=getParent(t, "tr" ).rowIndex || 0;
ray[row][col]=t.value;
el("ta1").value=ray.join("\n");
}//end function doRay update

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum