What I'm trying to achieve:
Receive both the Height & Weight values from the Bootstrap-Sliders and use these values to work out the BMI.
Current Problems:
Unable to get both the Height & Weight values from within each function due to the .change
event.
Open to suggestions and advice,
Thank you!
JS
$(function() {
// Weight
$('#weight').slider({
formatter: function(value) {
return value;
}
}).on('slideStop', function(ev){
$(this).val($(this).data('slider').getValue());
});
$('#weight').change(function() {
var weightval = $('#weight').val();
});
// Height
$('#height').slider({
formatter: function(value) {
return 'Height: ' + value;
}
}).on('slideStop', function(ev){
$(this).val($(this).data('slider').getValue());
});
$('#height').change(function() {
var heightval = $('#height').val();
});
// BMI
$('#bmi').text(weightval + heightval);
});
JS - Also Tried.
var heightval;
$('#height').change(function() {
heightval = $('#height').val();
});
$('#bmi').text(heightval);
HTML
<input id="weight" data-slider-id='weightSlider' type="text" data-slider-min="50" data-slider-max="150" data-slider-step="1" data-slider-value="0"/>
<input id="height" data-slider-id='heightSlider' type="text" data-slider-min="100" data-slider-max="250" data-slider-step="1" data-slider-value="0"/>
JS - Anwser
$(function() {
// Weight
$('#weight').slider({
formatter: function(value) {
return value;
}
}).on('slideStop', function(ev){
$(this).val($(this).data('slider').getValue());
});
$('#weight').change(updateBmi);
// Height
$('#height').slider({
formatter: function(value) {
return 'Height: ' + value;
}
}).on('slideStop', function(ev){
$(this).val($(this).data('slider').getValue());
});
$('#height').change(updateBmi);
function updateBmi(){
var heightval = $('#height').val(); // Height
var weightval = $('#weight').val(); // Weight
var bmicalc = (weightval/heightval)/heightval; // BMI Calc
var bmi = Math.round(bmicalc * 10) / 10; // Decimal
$('#bmi').text(bmiround); // Display Value
}
});
Live URL
RawGit
Assuming I got the question right (if not, please post a jsfiddle or jsbin sample), I think you can use a single handler for the change:
$('#weight').change(updateBmi);
$('#height').change(updateBmi);
function updateBmi(){
var heightval = $('#height').val();
var weightval = $('#weight').val();
$('#bmi').text(weightval + heightval);
}