my first post so please be gentle. I have a range slider with min and max from 1-100. My price structure ranges from £1.99 to say £1.19 dependant on how many the user selects on the range slider. I'd like to know the best way to achieve this, either by a switch statement, or using if else statements. The value chosen will be validated on the post back via php so I'm not concerned about security in this question. Apologies if I've missed something, look forward to the help.
$(document).ready(function() {
$("#slider").slider({
range: "min",
animate: "slow",
value:1,
min: 1,
max: 100,
step: 1,
slide: function(event, ui) {
update(1,ui.value); //changed
}
});
//Inital values when page loads, amount is set to 1 as this is the minimum a user can buy.
$("#amount").val(1);
$("#price").val(1.99);
$("#amount-label").text(0);
$("#price-label").text(0);
update();
});
//changed. now with parameter
function update(slider,val) {
//changed. Now, directly take value from ui.value. if not set (initial, will use current value.)
var $amount = slider == 1?val:$("#amount").val();
var $price = $("#price").val();
$total = "£" + ($amount * $price).toFixed(2);
$( "#amount" ).val($amount);
$( "#amount-label" ).text($amount);
$( "#price" ).val($price);
$( "#price-label" ).text($price);
$( "#total" ).val($total);
$( "#total-label" ).text($total);
$('#slider a').html('<label><span class="glyphicon glyphicon-chevron-left"></span> '+$amount+' <span class="glyphicon glyphicon-chevron-right"></span></label>');
}