0

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>
6
  • 1
    Can you add the HTML, so we can see where the values are coming from? Commented Dec 18, 2013 at 16:38
  • I added the html for clarity Commented Dec 18, 2013 at 16:46
  • Robbert's answer I believe is correct. You need to define the variable outside of a function if you're going to be using it in other functions within the same scope. Commented Dec 18, 2013 at 16:47
  • Thanks to Robert and everyone else! That worked. If I wanted to add a third variable would I need to define it outside and set to 0 as well? Commented Dec 18, 2013 at 16:52
  • It really depends on where you intend to use it. If it needs to be used in multiple places, then putting it at the top of the script is the right place for it. BTW, setting the value to 0 just insures that the variable has a value before it is called and avoids any undefined errors that might arise otherwise. Commented Dec 18, 2013 at 16:54

1 Answer 1

3

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
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.