I have a label, costLabel.
What I want to be able to do is change the value of this label depending on the selected value of a dropdownlist.
This is my HTML:
<table>
<tr>
<td width="343">Package*</td>
<td colspan="4">
<select class="purple" name="package">
<option value="standard">Standard - €55 Monthly</option>
<option value="standardAnn">Standard - €49 Monthly</option>
<option value="premium">Premium - €99 Monthly</option>
<option value="premiumAnn" selected="selected">Premium - €89 Monthly</option>
<option value="platinum">Platinum - €149 Monthly</option>
<option value="platinumAnn">Platinum - €134 Monthly</option>
</select>
</td>
<tr>
<td width="343">
<p>We bills quarterly/annually in advance</p>
<p>See <a href="#dialog" name="modal">Pricing</a> for more details</p>
</td>
<td colspan="4"><label id="costlabel" name="costlabel">Total Cost:</label></td>
<tr>
</table>
The values that go into the cost label are
I did have the following code in place which calculated the cost depending on the dropdown menu, but the registration form has since changed to be more simpler(i.e. discountselection is not gone), and I'm having a bit of trouble adapting the jQuery. Can someone help?
$(function() {
$("#discountselection, select[name=package], input[name=discount]").
change(function() {
var
selected_value = $("#discountselection").val(),
discount = [12, 24, 36],
package = $("select[name=package]").val(),
package_prices = {'standard': 49, 'premium': 85, 'platinum': 134 },
cost = package_prices[package] * discount[selected_value-1];
$("#costlabel").val(cost);
});
});
To dynamically update the Label widget, we can use either config(**options) or an inline configuration method such as for updating the text, we can use Label["text"]=text; for removing the label widget, we can use pack_forget() method.
Press Ctrl+click to select the label. Right-click and select Label Properties or Properties. In the Properties palette, change the desired properties.
jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.
I seem to have a blind spot as regards your html structure, but I think that this is what you're looking for. It should find the currently-selected option from the select
input, assign its text to the newVal
variable and then apply that variable to the value
attribute of the #costLabel
label:
$(document).ready(
function() {
$('select[name=package]').change(
function(){
var newText = $('option:selected',this).text();
$('#costLabel').text('Total price: ' + newText);
}
);
}
);
<form name="thisForm" id="thisForm" action="#" method="post">
<fieldset>
<select name="package" id="package">
<option value="standard">Standard - €55 Monthly</option>
<option value="standardAnn">Standard - €49 Monthly</option>
<option value="premium">Premium - €99 Monthly</option>
<option value="premiumAnn" selected="selected">Premium - €89 Monthly</option>
<option value="platinum">Platinum - €149 Monthly</option>
<option value="platinumAnn">Platinum - €134 Monthly</option>
</select>
</fieldset>
<fieldset>
<label id="costLabel" name="costLabel">Total price: </label>
</fieldset>
</form>
Working demo of the above at: JS Bin
val() is more like a shortcut for attr('value'). For your usage use text() or html() instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With