/    Sign up×
Community /Pin to ProfileBookmark

adding dynamic values…

I wonder if you can help me.
I have a web page with a form which has dynamic named fields for size, quantity, price. The reason they are dynamic is that there could be one occurance of them, or 10 occurrances, as it depends how many options they picked on a previous page. they are named in the following way. size0, size1, size2, size3, price0, price1, price2, etc.

I have tried to write a function to work out the value of price, which is size*quantity

here is my code

function doMath(i) {
c = “document.form1.price”+i+”.value”
a = “document.form1.size”+i+”.value”
b = “document.form1.quantity”+i+”.value”

document.form1.price+i+.value = eval(a + ‘*’ + b)
}

and from the page the form elements look like this
<select name=”size[<%=i%>]” onchange=”doMath(‘<%=i%>’)”>
<option value=”0″>Select</option>
<option value=”6″>7.5&quot;x5&quot; @ &pound;6.00</option>
<option value=”8″>10&quot;x5&quot; @ &pound;8.00</option>
</select>
the <%=i%> is changed to 0, 1, 2, 3, etc on the servergiving size0, size1 and doMath(‘0’), doMath(‘1’) etc.

I can get the function to work for the first line if I change the last line to
document.form1.price0.value = eval(a + ‘*’ + b)

So I suppose I need to know how to get the value i I pass to the function in instead of the 0 above?

Not sure if this is clear but hope you can help

Richard

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMay 18.2004 — Hi!

No need to use "eval()" here. Your function could simply look like this:

function doMath(i) {

a = document.form1["size"+i].value;

b = document.form1["quantity"+i].value;

document.form1["price"+i].value = Number(a)*Number(b);

}

Cheers - Pit
Copy linkTweet thisAlerts:
@squarefishauthorMay 18.2004 — Thanks Pitt - jusst the job.

I don't suppose you would know how to add up my totals now do you?

I have price0 to price{i} - i could be anything upto about 20???

So I need

total = price0 + price1 + price2 -> price10 + postage

Thanks again

Richard
Copy linkTweet thisAlerts:
@PittimannMay 18.2004 — Hi!

Not knowing, what you already have in your document (form action, validation etc.) just a sample function, which you will have to call:

function calcTotal(frm){

var tInps=frm.elements;

var tot=0;

for (var i=0;i<tInps.length;i++){

if(tInps[i].name.substring(0,5)=="price"){

tot+=Number(tInps[i].value);

}

}

alert(tot);

}



button example to call it (button has to be an element in the same form):



<input type="button" onclick=calcTotal(this.form)" value="show total">



Cheers - Pit
Copy linkTweet thisAlerts:
@squarefishauthorMay 19.2004 — Thanks Pitt perfect!
Copy linkTweet thisAlerts:
@PittimannMay 19.2004 — Hi!

You're welcome! :p

Cheers - Pit
×

Success!

Help @squarefish spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...