Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use a loop? 1

Status
Not open for further replies.

hd65

Technical User
Aug 17, 2000
27
US
here is the script that works but I would need to enter about 500 if else statements and the same number of totals[*]. Is there a way I can shorten the script?
---------------------------------------------
function total() {
var totals = new Array();
if (document.total.pic1001){
totals[1] = document.total.pic1001.value;
}else{
totals[1] = "0";}
if (document.total.pic1002){
totals[2] = document.total.pic1002.value;
}else{
totals[2] = "0";}
if(document.total.pic1003){
totals[3] = document.total.pic1003.value;
}else{
totals[3] = "0";}
document.total.ordertotal.value = (totals[1])*1 + (totals[2])*1 + (totals[3])*1;
}
-----------------------------------------
thanks paul
 
here, phew:

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Untitled</title>
<script>
function b(){
//somenum should equal the total number of fields you
//want to check and they must be named similarly
somenum=2
var totals=new Array();var cnt=1
for(var i=0;i<somenum;i++)
{
var fld=eval(&quot;document.total.pic100&quot;+cnt+&quot;.value&quot;)
if(fld)
{
totals[totals.length]=fld
}
// alert(cnt)
cnt++
}
totalamt=0
for(var j=0;j<totals.length;j++)
{
totalamt+=new Number(totals[j])
}

document.total.ordertotal.value=totalamt
}
</script>
</head>

<body>
<form name=&quot;total&quot;>
<input type=text name=&quot;pic1001&quot;>
<input type=text name=&quot;pic1002&quot;>
<input type=text name=&quot;ordertotal&quot;>
</form>
<button onClick=&quot;b()&quot;></button>
</body>
</html>
jared@aauser.com
 
What if some of the objects are not used.ie
If <input type=text name=&quot;pic1001&quot;> and <input type=text name=&quot;pic1003&quot;>
are the only ones.
I get the error 'document.total.pic1002.value' is not an object.
In my script the setting the totals[2]=&quot;0&quot; takes care of it.

thanks
paul




 
make sure you make the objects in order... jared@aauser.com
 
I cant controll that the file is generated (CGI)
the objects are determined by a user selection.
bummer huh?

thanks
paul
 
You need to somehow be able to set the somenum variable to include the correct ammount of fields...then this will work even if you misname them

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Untitled</title>
<script>
function b(){
//somenum should equal the total number of fields you
//want to check and they must be named similarly
somenum=4
var totals=new Array();var cnt=1
for(var i=0;i<=somenum;i++)
{

var fld=eval(&quot;document.total.pic100&quot;+cnt)
if(typeof(fld)==&quot;undefined&quot;){cnt++;continue}
if(fld.value)
{
totals[totals.length]=fld.value
}
// alert(cnt)
cnt++
}
totalamt=0
for(var j=0;j<totals.length;j++)
{
totalamt+=new Number(totals[j])
}

document.total.ordertotal.value=totalamt
}
</script>
</head>

<body>
<form name=&quot;total&quot;>
<input type=text name=&quot;pic1001&quot;>
<input type=text name=&quot;pic1002&quot;>
<input type=text name=&quot;pic1004&quot;>
<input type=text name=&quot;pic1005&quot;>
<input type=text name=&quot;ordertotal&quot;>
</form>
<button onClick=&quot;b()&quot;></button>
</body>
</html>
jared@aauser.com
 
Thanks I'll have to look at the other scripts to see if I can find a way to set somenumber.

thanks again.
paul
 
maybe I can find a way to do it... are those the only textboxes the script generates, if so you can use the form.elements array to loop through all the elements, see if their textboxes and only add values if they are... i gotta go home (stillo at work) or my girlfriend will kill me, so cya monday :eek:) jared@aauser.com
 
No........

but if you think of anything just let me know
if not thats ok too.

paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top