views:

24

answers:

2

I'm completely new to javascript and I was wondernig if someone could help me with probably a simple query!

I have the following code: http://jsfiddle.net/J47E6/

I've managed to get the hide/show functions to work, but what I'm struggling with is getting the price to display in the label.

Can someone point me in the right direction?

+1  A: 

Have a look at: revised code

There were a number of errors in JS and inconsistencies in the markup.

Thomas
Thanks Thomas.. I'm also just wondering, is there any code I can change so that it does the calculations in the label immediately, and doesn't wait for the dropdownlist to change. For example, in this code, if I run it, and click yes, nothing appears in the label until I change the dropdownlist.
TaraWalsh
Sure, see: http://jsfiddle.net/J47E6/19/
Thomas
Thanks Thomas, that's just what I need :)
TaraWalsh
+2  A: 

New version of your script is now corrected. I noticed you missed an id selector in your jQuery (#) for discountSelection.

The method now works, but problem is, I'm unsure of how to caculate your math therefor I cannot complete it.

$('#discountselection').hide();
$('#costlabel').hide();

$('#No').click(function() {
    $('#discountselection').hide();
    $('#costlabel').hide();
});

$('#Yes').click(function() {
    $('#discountselection').show();
    $('#costlabel').show();
});


$("#discountselection").change(function()  { 

var selected_value = $("#discountselection option:selected").val();

alert("Selected Value = " + selected_value);
var discount = {1: 12, 2: 24, 3: 36};

var package_prices = {'standard': 45, 'premium': 85, 'platinum': 134 };

var cost = 2; //package_prices[package] * discount[discountselection];
alert("Cost " + cost);
$("#costlabel").val(cost);

}); ​

Where does 'package' and 'discountSelection' come from. answer those and this'll be done for you.

package_prices[package] * discount[discountselection];
Glycerine
@Glycerine, thanks for the reply. package is a drop down list in the HTML form, discount selection is another dropdownlist in the HTML form. Perhaps they should be #discountselection, and #package (I will add in and 'id' to package)?
TaraWalsh