Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  22] [ 2]  / answers: 1 / hits: 19073  / 13 Years ago, fri, may 13, 2011, 12:00:00

I've a variable called numbers in javascript and this variables holds 10 numbers from 0 to 9 as shown below.



var numbers = 0123456789;


Now what I want to be able to do is assigning each of these numbers to an input text field using document.getElementByID(a) = ;, for example:



<input type=text id=a value= />
<input type=text id=b value= />
<input type=text id=c value= />
<input type=text id=d value= />
<input type=text id=e value= />
<input type=text id=f value= />
<input type=text id=g value= />
<input type=text id=h value= />
<input type=text id=i value= />
<input type=text id=j value= />


Currently the following text fields above holding no values, but I want to be able to assign each of the numbers in variable numbers to each of the text fields above, so it would looks like this when user clicks on a button called click me.



<input type=text id=a value=0 />
<input type=text id=b value=1 />
<input type=text id=c value=2 />
<input type=text id=d value=3 />
<input type=text id=e value=4 />
<input type=text id=f value=5 />
<input type=text id=g value=6 />
<input type=text id=h value=7 />
<input type=text id=i value=8 />
<input type=text id=j value=9 />


Also is there any way to leave a text field empty if a particular number is = 0



Thanks in advance :)


More From » dom

 Answers
2
var numbers = 0123456789;
var ids = [a, b, c, d, e, f, g, h, i, j];

for (var i = 0; i < ids.length; i++)
{
var el = document.getElementById(ids[i]);
var num = numbers[i];

if (num == 0)
el.value = ;
else
el.value = num;
}


Working sample: http://jsfiddle.net/LrJ5S/.


[#92252] Thursday, May 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
calicinthias

Total Points: 447
Total Questions: 101
Total Answers: 118

Location: Botswana
Member since Sat, Dec 31, 2022
1 Year ago
calicinthias questions
Sun, Jan 2, 22, 00:00, 2 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Mon, Aug 10, 20, 00:00, 4 Years ago
;