View Full Version : getting array number from a textbox
hi can anyone show me how one can get an array number from maybe a textbox
if the textbox value="5"
how can you get this to an array and make the array number to be "5" eg
myarray[5] the number "5" was got from a textbox.
pliz help. urgent
BonRouge
04-12-2007, 09:25 AM
I'm sorry... Can you say that again?
ok maybe i have an array
var arr=new Array()
arr[1]="hi";
arr[2]="there";
arr[3]="haha";
and i have a form with a textbox and a button. so when someone enters the value 1 in the textbox and presses the button the arr[1] is called into an alertbox or if he enters 2 and presses the button the arr[2] is called into an alertbox and so on.
so the question is passing the value of the textbox to be the array value e.g.
arr[value of the textbox] i think thats alittlebit clear.
BonRouge
04-12-2007, 09:59 AM
How about something like this?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Quick example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background:#fff;
}
</style>
<script type="text/javascript">
var arr=new Array()
arr[1]="hi";
arr[2]="there";
arr[3]="haha";
function fill(sel,p) {
var selBox=document.getElementById(sel);
var textBox=document.getElementById(p);
selBox.onchange=function() {
while(textBox.childNodes[0]) {
textBox.removeChild(textBox.childNodes[0]);
}
if(arr[this.value]) {
var text=arr[this.value];
var op=document.createElement('option');
var textN=document.createTextNode(text);
op.appendChild(textN);
textBox.appendChild(op);
}
}
}
window.onload=function() {fill('numbers','text')}
</script>
</head>
<body>
<p><select id="numbers">
<option>- select one --</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select></p>
<p id="text"></p>
</body>
</html>
(I know it's not the same as what you said, but it's very similar...)
pretty advanced but thank you. which site would i get a quick tutorial about
childNode, appendChild all about these Nodes stuff
BonRouge
04-12-2007, 10:36 AM
Quirksmode (http://www.quirksmode.org/dom/w3c_core.html)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.