PDA

View Full Version : How to return a value from a function to a textbox


Stoffel
01-30-2003, 05:53 PM
Hiya

I've got a function where I calculate a price,
I work with variables so I can use the same function several times,
The function calculates the price perfect, but it wont return the price to its destination, I also tried with "return price" in the function, but that won't work either

Anyone knows what I've done wrong?

boywonder
01-30-2003, 06:59 PM
in your function set the value of the text box.

document.yourformname.value = price

if you want to vary the destination for calculated prices you can pass a reference to the destination field when you call the function.

hope that helps - if I misunderstood you can you post some code...

Stoffel
01-30-2003, 08:46 PM
Thats the problem,

I am trying to leave the document.formname.textbox.value out of it because it would be a lot easyer to calculate things.

this is what I have right now:

pricecalculation.js

function priceCalculation(number,type,film,vhs,dvd,price) {
var price1, total;

if ( (type != 0) && (number!= 0) && (film != '' ))
{
price1= ( type == "VHS" ) ? parseFloat(vhs) : parseFloat(dvd);
total = Math.round( number * price1 * 100 ) / 100
price = "€ " + total;
return prijs;
}
else
price = "";
return prijs;
}


And this is a part of the form


<select name="type1" class="midden" onChange="priceCalculation(
document.formulier.number1.options[document.formulier.number1.selectedIndex].value,
document.formulier.type1.options[document.formulier.type1.selectedIndex].value,
document.formulier.vhs1.value,
document.formulier.dvd1.value,
document.formulier.price1.value)">
<option value="0" selected>[Type]</option>
<option value="VHS">VHS</option>
<option value="DVD">DVD</option>
</select>
<select name="number1" class="midden" onchange="prijsBerekening(
document.formulier.number1.options[document.formulier.number1.selectedIndex].value,
document.formulier.type1.options[document.formulier.type1.selectedIndex].value,
document.formulier.vhs1.value,
document.formulier.dvd1.value,
document.formulier.prijs1.value)">
<option value="0">[Aantal]</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="hidden" name="vhs1">
<input type="hidden" name="dvd1">
<input type="text" name="prijs1" value="" size="10" class="midden" onfocus="this.blur()">


I hope this makes things clearer and that u can help me

Greetz

boywonder
01-30-2003, 11:13 PM
If your goal is to make the value calculated by the function appear in the text box prijs1 then why wouldn't the following work? It doesn't have any effect on the calculation.
<script language="JavaScript" type="text/JavaScript">
function priceCalculation(number,type,film,vhs,dvd,price) {
var price1, total;

if ( (type != 0) && (number!= 0) && (film != '' ))
{
price1= ( type == "VHS" ) ? parseFloat(vhs) : parseFloat(dvd);
total = Math.round( number * price1 * 100 ) / 100
price = "€ " + total;
document.formulier.prijs1.value = price;
}
else
price = "";
document.formulier.prijs1.value = price;
}
</script>

does that help at all?

whammy
01-31-2003, 12:13 AM
And you thought my script was hard to modify? If I were you I'd definitely take a second look at it - it's much simpler and to the point than this.

What doesn't it do that you would like it to? :confused:

Although, the above should work...

How about you email me a link to your page? I'm sure I could get this working in a much simpler fashion...

Stoffel
02-01-2003, 11:04 AM
Hiya,

I know it works that way Boywonder, because thats the way I did it first, but if I want to do it that way, I need 5 different functions, and I only wanted to do it in 2 functions. (1 for the films and one for the CDs)

I'll give u the link, but the pages are in dutch.
http://users.pandora.be/thelord/EindWerk-/WeBsiTe-/ned/verkoop.html

At the bottom of the page you can enter the movies u want, you have to push 'Klik hier' for a list of movies (only the first one works, I am still working on it).
At the moment the first movie won't work because thats the one I tried to modify.

I hope u can help me :)
And if it isn't possible, I'll work with the 5 functions, its not a disaster :D