0

How do I use a variable declared in function in another function.

I need the mortgageLimit, mortgageMax, and mortgageStart to be declared when the user clicks the button with the step4 class

$(".step4").click(function() {
   var mortgageLimit = parseInt(document.getElementById("property_value").value) * .80;
   var mortgageMax = parseInt(mortgageLimit * 1000);
   var mortgageStart = mortgageMax * .5;
});

Then I'd like to use mortgageMax and mortgageStart as the min and max of a slider I'm using

var thirdSlider = document.getElementById("estimated_mortgage_value");
noUiSlider.create(thirdSlider, {
    'start': ["220000"],
    'connect': 'lower',
    'format': wNumb({
        decimals: 0,
        thousand: ',',
     }),
     'range': {
         'min': [mortgageStart],
         'max': [mortgageMax]
     },
     'step': 10000
});

I keep on getting a "Uncaught ReferenceError: mortgageStart is not defined" error. I've tried declaring the mortgageStart and mortgageMax as a global variable, but I need the variables declared when the user clicks, so the variables need to be declared within $(".step4").click(function() {

  • 1
    Please google your title before asking – mplungjan Oct 27 '17 at 17:45
  • If you declare the variables in the global scope you can access them within any function at any given time but if you really need to declare them within a function then you will no doubt have to call the other function and pass them in the function call after declaring them, for example: `NextFunction(mortgageLimit,mortgageMax,mortgageStart)` – NewToJS Oct 27 '17 at 17:48

1 Answers1

2

Declare the variable in the global scope first.

var mortgageMax;
var mortgageStart;
var mortgageLimit;

$(".step4").click(function() {
   mortgageLimit = parseInt(document.getElementById("property_value").value) * .80;
   mortgageMax = parseInt(mortgageLimit * 1000);
   mortgageStart = mortgageMax * .5;
});
jcarapia
  • 578
  • 1
  • 7
  • 20