Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi frnds have a porb with javascript iterations on names
situvation:
var test1=document.getElementById('a').value;
var test2=document.getElementById('b').value;
var test3=document.getElementById('c').value;
var test4=document.getElementById('d').value;
function empty()
{
for(var i=0;i<=3;i++)
{
if(test[i].length<=0)
{
alert("empty"+test[i]);
}
}
}this code shows me an error saying test undeclared variable and empty undefined :(
plz help me out asap
programming newbie :)

var test = new Array();
test[0]=document.getElementById('a').value;
test[1]=document.getElementById('b').value;
test[2]=document.getElementById('c').value;
test[3]=document.getElementById('d').value;
function empty()
{
for(var i=0; i<=test.length; i++)
{
if(!test[i])
{
alert("empty"+test[i]);
}
}
}Michael J

Error: missing ; before statement
Source file: file:///C:/xxx.html
Line: 21, Column: 4
Source code:var test[0]=document.getElementById('a').value;same error for all elements of test.. :(
programming newbie :)

Hi,
It's because the 'test' array is getting built before the page has loaded (so the input fields aren't yet defined).You can build the test table each time you call 'empty()' (this also grabs the correct values) or build the table once on body onload and then retrieve the values in your for loop:
<html>
<head>
<script language="javascript">var test = new Array();
function buildArray()
{
test[0]=document.getElementById('a');
test[1]=document.getElementById('b');
test[2]=document.getElementById('c');
test[3]=document.getElementById('d');
}function empty()
{
for(var i=0; i<=test.length; i++)
{
if(!test[i] || !(test[i].value))
{
alert("empty"+test[i]+" ("+i+")");
}
}
}
</script>
</head>
<body onload="buildArray()">
<input id="a" type="text"></input>
<input id="b" type="text"></input>
<input id="c" type="text"></input>
<input id="d" type="text"></input>
Call empty()
</body>
</html>hth
Elinor
Elinor Hurst
http://elinorhurst.blogspot.com

thanks a lot..
solved the problem earlier but now i know the detailed view of error i made...
thanks a lot
programming newbie :)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |