Computing.Net > Forums > Web Development > Javascript text boxes with array

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Javascript text boxes with array

Reply to Message Icon

Name: raviepic3
Date: September 13, 2008 at 02:18:17 Pacific
OS: XP
CPU/Ram: 1GB
Product: Gigabyte
Comment:

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 :)



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: September 13, 2008 at 22:05:31 Pacific
Reply:

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


0

Response Number 2
Name: raviepic3
Date: September 14, 2008 at 04:49:56 Pacific
Reply:

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 :)


0

Response Number 3
Name: raviepic3
Date: September 14, 2008 at 04:58:28 Pacific
Reply:

and also says an error like


empty is not defined

programming newbie :)


0

Response Number 4
Name: Elinor
Date: September 16, 2008 at 03:49:51 Pacific
Reply:

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


0

Response Number 5
Name: raviepic3
Date: September 16, 2008 at 20:40:04 Pacific
Reply:

thanks a lot..

solved the problem earlier but now i know the detailed view of error i made...

thanks a lot

programming newbie :)


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Javascript text boxes with array

PHP Search text box www.computing.net/answers/webdevel/php-search-text-box/1741.html

Multiline text box control www.computing.net/answers/webdevel/multiline-text-box-control/1361.html

PHP/MySQL include www.computing.net/answers/webdevel/phpmysql-include/3262.html