Reputation: 1725
I am trying to add two values taken from jQuery elements together and I cannot understand what Im doing incorrectly. Here is my code:
$(document).on('pageinit', function() { //init and start function
$("#saveHospital").click(function(){ //run function on button click
var Hospitalval = $("#Hospital").val(); //assign value from element id #Hospital, which is a slider
});
$("#saveICU").click(function(){ //run function on button click
var ICUval = $("#ICU").val(); //assign value from element id #Hospital, which is a slider
var surgicalProc = parseInt(Hospitalval) + parseInt(ICUval); //parse both strings to integers and add them together
$('.P14').html(surgicalProc); //set selector to display the value of the variable
});
This is my html where the value is to be displayed:
<p class="P14">Total calculation</p>
Im just getting the string 'total calculation' instead of the value of surgicalProc. FYI If I replace the two variables being parsed with numbers the function works so I know the error is somewhere in the definition, but I cannot see it.
Any thoughts?
Here is the hmtl where the elements are defined:
Sliders
<input type="range" name="Hospital" id="Hospital" value="60" min="0" max="100" />
<input type="range" name="ICU" id="ICU" value="60" min="0" max="100" />
Buttons
<a href="#pageThree" id="saveHospital" data-role="button" data-theme="b" data-transition="slide">Submit</a>
<a href="#pageFour" id="saveICU" data-role="button" data-theme="b" data-transition="slide">Submit</a>
Upvotes: 0
Views: 68
Reputation: 6592
I think you may be dealing with variable scope issues here. Hospitalval
is defined in one function and you call it in another.
This may work for you
var Hospitalval = 0;
$(document).on('pageinit', function() { //init and start function
$("#saveHospital").click(function(){ //run function on button click
Hospitalval = $("#Hospital").val(); //assign value from element id #Hospital, which is a slider
});
$("#saveICU").click(function(){ //run function on button click
var ICUval = $("#ICU").val(); //assign value from element id #Hospital, which is a slider
var surgicalProc = parseInt(Hospitalval) + parseInt(ICUval); //parse both strings to integers and add them together
$('.P14').html(surgicalProc); //set selector to display the value of the variable
});
Upvotes: 3