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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for loop question for a newbie

Status
Not open for further replies.
Oct 29, 2004
5
0
0
US
Hi javascript gurus,

How can I make it so that "function calculate()", "cost", "qty" and "value" become...

calculate1(), cost1, qty1, value1
calculate2(), cost2, qty2, value2...etc?

<script language="JavaScript">
for (i = 0; i <= 10; i++)
function [red]calculate[/red]()
{
var a = document.MyForm.[red]cost[/red].value;
var b = document.MyForm.[red]qty[/red].value;
var result = eval(a) * eval(b);
document.MyForm.total.[red]value[/red] = result;
}
</script>
 
one way:

Code:
function calculate() {
    var a, b, result;
    for (var i = 0; i < 11; i++) {
       a = document.MyForm.elements['cost' + i].value;
       b = document.MyForm.elements['qty' + i].value;
       result += (eval(a) * eval(b));
    }
    document.MyForm.total.value = result; 
}

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Thanks cLFlaVA

But I was getting errors on that one.

What I'm trying to accomplish is...

<% For I = 1 To 10 %>
<tr>
<td>name="cost<%=I%>" value="<%=ncost(I)%>" type="text"></td>
<td><input class="frm2" name="qty<%=I%>" value="<%=nqty(I)%>" type="text" OnChange="calculate<%=I%>()"></td>
<td>input name="total<%=I%>" value="<%=ntotal(I)%>" type="text"></td>
</tr>
<% Next %>

So I needed "function calculate()", "cost", "qty", "total" to loop.
 
maybe
<%i=1;
while (i<11) {%>
<tr>
<td>name="cost<%=I%>" value="<%=ncost(I)%>" type="text"></td>
<td><input class="frm2" name="qty<%=I%>" value="<%=nqty(I)%>" type="text" OnChange="calculate<%=I%>()"></td>
<td>input name="total<%=I%>" value="<%=ntotal(I)%>" type="text"></td>
</tr>
<%
i++;
}

[conehead]
 
is this in the right forum?

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
I hope so cLFlaVA,

If I do each function one at a time (like below)the "fuction calculate()" will work with "OnChange="calculate<%=I%>()".

I wanted to use a for loop on this to keep it clean.

On another note, how to add numeric validation to it?

function calculate1()
{

var a = document.MyForm.cost1.value;
var b = document.MyForm.qty1.value;
var result = eval(a) * eval(b);

document.MyForm.total1.value = result;
}

then...

function calculate2()
{

var a = document.MyForm.cost2.value;
var b = document.MyForm.qty2.value;
var result = eval(a) * eval(b);

document.MyForm.total2.value = result;
}

an so on...
 
This is not the best solution. What if you had 10,000 iterations? Would you want to create 10,000 functions? It would be much better to write ONE function and then loop through the iterations INSIDE the function, like I showed up above.

As for "but I was getting errors", that wasn't a very specific description. I don't call up a mechanic and say "I am having problems with my car." Instead, I say, "there is a knocking when I start my car", or, "The Check Engine light keeps coming on".

Where are you attempting to call these functions from? How are you trying to call them?

You certainly only need one function - the problem is how / when you're calling them.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
My bad for being so vague cLFlaVA. I really appreciate your (and everyones) help.

But yeah just like I had it...

I have this for loop on my asp page where the user enters cost & qty in the fields and then it automatically calculates the total and displays it...

<% For I = 1 To 10 %>
<tr>
<td><input name="cost<%=I%>" value="<%=ncost(I)%>"></td>
<td><input name="qty<%=I%>" value="<%=nqty(I)%>"onchange="[red]calculate<%=I%>[/red]"></td>
<td><input name="total<%=I%>" value="<%=ntotal(I)%>"></td>
</tr>
<% Next %>

this will output...
<tr>
<td><input name="cost1" value="cost1"></td>
<td><input name="qty1" value="qty1"></td>
<td><input name="total1" value="total1" onchange="[red]calculate1[/red]"></td>
</tr>
<tr>
<td><input name="cost2" value="cost2"></td>
<td><input name="qty2" value="qty2"></td>
<td><input name="total2" value="total2" onchange="[red]calculate2[/red]"></td>
</tr>
and so on...

What I'm trying to get with function Calculate+i() is
cost * qty = value for each loop.

<script language="JavaScript">
for (i = 0; i <= 10; i++)
function calculate()
{
var a = document.MyForm.cost.value;
var b = document.MyForm.qty.value;
var result = eval(a) * eval(b);
document.MyForm.total.value = result;
}
</script>

I wanted to get function calculate+i(), cost+i, qty+i, total+i to loop

When I tried your suggestion I'd get

Error: Object Expected

it's not doing a loop for function Calculate+i()?

function [red]Calculate1[/red]()
function [red]Calculate2[/red]()
function [red]Calculate3[/red]()
etc...

Hope I explained it well.
 
I'm going to have to agree with clflava on this one...
It's a bad idea to have code write more code. Consider sending values to the JavaScript function rather than creating more functions on the fly:
Code:
//javascript code:
function calculate(cost,qty,target) {
    target.value = cost * qty;
}
Code:
asp code:
<% For i = 1 to 10 %>
<td><input name="cost<%= i %>" value="<%= ncost(i) %>"></td>
<td><input 
    name="qty<%= i %>" 
    value="<%= nqty(i) %>"
    onchange="calculate(document.formName.cost<%= i %>.value, document.formName.qty<%= i %>.value, document.formName.total<%= i %>);">
</td>
<td><input name="total<%= i %>" value="<%= ntotal(i) %>"></td>
From that ASP code, you should get this:
Code:
<td><input name="cost1" value="cost1"></td>
<td><input 
    name="qty1"
    value="qty1"
onchange="calculate(document.formName.cost1.value,document.formName.qty1.value, document.formName.total1);"></td>
<td><input name="total1" value="total1"></td>

... etc

Hope this helps,

Jon

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
Ooops...

in the asp code, I forgot to put <%= Next %> to continue the For Loop...

sorry

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
jtreefrog,

Your suggestion worked great. Thanks! I see what you guys say about having code write more code (not good).

Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top