views:

57

answers:

2

I'm trying to create a page where there are two dropdown menus, and then when the user presses submit, it calculates a price based on the data given from the dropdown menu. It needs to take either 1 or 2 from the first dropdown menu choice, multiply that by the value of the second dropdown menu choice, multiply that by 0.2, and output it when the user presses submit. I've tried to make it work but I keep getting NaN errors. Any help?

<head>
<script type="text/javascript">
function price() {
        var sqBn = document.priceCalc.squareOrBanner;
        var Amt = document.priceCalc.number;
    var price = sqBn * Amt * 0.2;
    document.write(price);
}
</script>

<form name="priceCalc" action="priceCalcPage.html" onsubmit="document.write(price())">
<div align="center">
<select name="squareOrBanner">
<option value="1">Square</option>
<option value="2">Banner</option>
</select>

<select name="number">
<option value="250">250</option>
<option value="500">500</option>
<option value="750">750</option>
<option value="1000">1000</option>
</select>
<br><input type="submit" value="Figure out pricing!"><br>
</div>
</form>
+1  A: 

Maybe you are looking for something like this:

http://jsfiddle.net/JSvSn/1/

You need to access the value property in those objects:

var price = sqBn.value * Amt.value * 0.2;

EDIT: Here's a version that updates the value of an input box with the price: http://jsfiddle.net/JSvSn/5/

EDIT 2: Show price in a div.

quantumSoup
Thank you, that worked. How do I make the value appear without the page refreshing? Like adding it below the form rather than it being the only thing when the button is pressed?
Andrei Korchagin
you can use onchange or onselect event of the select list to run your calculation.
James Lin
onchange works halfway the way I want it, but it still keeps refreshing the page to show the value returned rather than outputting it after the form.
Andrei Korchagin
@Andrei Read updated post ^
quantumSoup
.value for select boxes doesn't work across browsers
bluesmoon
@bluesmoon It does when you have a value for each option element. I tested it on Safari, Chrome and FF. Can't do IE though, I am on OS X
quantumSoup
@Aircule Thanks so much, that's almost perfect. Just one last thing, is there a way I could output the number without it being in an input box like that? Like just as a plain number as far as the user is concerned?
Andrei Korchagin
@Andrei : yes, see edit
quantumSoup
@Aircule: IE is where it doesn't work.
bluesmoon
@bluesmoon: Just tested on IE8, and it works fine.
quantumSoup
@Aircule Thanks again!
Andrei Korchagin
what about IE6?
bluesmoon
@bluesmoon Honestly I have no clue. I had IE6 installed at some point, but it's gone.
quantumSoup
+1  A: 

I'm not sure if the sqBn.value will work on all browsers. It would be safer to use

sqBn.options[sqBn.selectedIndex].value

and if you are still getting NaN results then try

parseInt(sqBn.options[sqBn.selectedIndex].value)

If you want to do this calculation without refreshing the page change the form declaration like this:

<form name="priceCalc" action="priceCalcPage.html" onsubmit="document.write(price());return false;">    

In this way the onsubmit will be executed, but won't submit anything to the action. If you want to submit it, you can still do it from javascript (document.priceCalc.submit()).

Zsolti