ahaberman25 Posted December 5, 2012 Share Posted December 5, 2012 How can I use a var established in another function: function updateTotal(){ var total = 0; for(var i=1; i<10; i++){ if(!isNaN(jQuery('#txt'+i).val())){ total = (parseInt(total) + parseInt(jQuery('#txt'+i).val())); } } jQuery('#total').text(total); }; function handleChange(input) { if (input.value < 1) input.value = 0; if (input.value > 3) input.value = 3; }; function results() { if ( total <= 6 ) { window.open('http://www.cre82morrow.com'); }if ( total == 7 || total == 8 ) { window.open('http://www.apple.com'); }else { window.open('http://www.chrome.com'); } }; The var total used in updateTotal() I am trying to use it again in function results, for some reason its not grabbing the var again. It only works if I put the if statement in the original function updateTotal() Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/ Share on other sites More sharing options...
trq Posted December 5, 2012 Share Posted December 5, 2012 Move this: var total = 0; out of the function. Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397630 Share on other sites More sharing options...
requinix Posted December 5, 2012 Share Posted December 5, 2012 (edited) You can't access variables defined in other functions. That's true for every programming language I can think of. Make updateTotal() return the value, then call it in results() to get that value. [edit] Or use it as window.total instead, I guess. Edited December 5, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397634 Share on other sites More sharing options...
codefossa Posted December 5, 2012 Share Posted December 5, 2012 As trq said, moving the variable outside of the function will allow you to access it in all functions. On a side note, you should use else if in your results() function for your second if statement. Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397657 Share on other sites More sharing options...
ahaberman25 Posted December 5, 2012 Author Share Posted December 5, 2012 (edited) Now its updating the total exponentially. Instead of the proper increments. if the var=0 is inside the function it works right? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="css/style.css" /> <script src="js/jquery-1.8.2.js"></script> <script src="js/jquery.validate.js"></script> <script type="text/javascript">// <![CDATA[ var total = 0; function updateTotal(){ for(var i=1; i<10; i++){ if(!isNaN(jQuery('#txt'+i).val())){ total = (parseInt(total) + parseInt(jQuery('#txt'+i).val())); } } jQuery('#total').text(total); }; function handleChange(input) { if (input.value < 1) input.value = 0; if (input.value > 3) input.value = 3; }; function results() { if ( total <= 6 ) { window.open('http://www.cre82morrow.com'); }if ( total == 7 || total == 8 ) { window.open('http://www.apple.com'); }else { window.open('http://www.chrome.com'); } }; </script> <title>Epsworth Sleep Quiz</title> <style> input { float: right; } #quizcontainer { width: 600px; } </style> </head> <body id="main-content"> <div id="quizcontainer"> <!-- start form --> <form id="myform" onsubmit=""> <fieldset> <legend><h3>Take the Sleep Disorder Self-Quiz Epworth Sleepiness Scale</h3></legend> <p><span style="font-size: 10pt;">How likely are you to doze off or fall asleep in the following situations, in contrast to just feeling tired? This refers to your usual way of life in recent times. Even if you have not done some of these things recently, try to work out how they would have affected you. Use the following scale and enter the number for each situation.</span></p> <div> <p style="text-align: left; margin-left: 180px;"><strong>0</strong> = no chance of dozing<br /> <strong>1</strong> = slight chance of dozing<br /> <strong>2</strong> = moderate chance of dozing<br /> <strong>3</strong> = high chance of dozing</p> </div> <h3><span style="text-decoration: underline; float:left;">SITUATION</span></h3> <h3><span style="text-decoration: underline; float:right;">Chance of dozing</span></h3> <br /> <br /> Sitting and reading <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt1" id="txt1" size="5" tabindex="1" value="0" /> <br /> <br /> Watching TV <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt2" id="txt2" size="5" tabindex="2" value="0" /> <br /> <br /> Sitting inactive in a public place (Such as a theatre or a meeting) <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt3" id="txt3" size="5" tabindex="4" value="0" /> <br /> <br /> As a passenger in a car for an hour without a break <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt4" id="txt4" size="5" tabindex="5" value="0" /> <br /> <br /> Lying down to rest in the afternoon when time permits <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt5" id="txt5" size="5" tabindex="6" value="0" /> <br /> <br /> Sitting and talking to someone <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt6" id="txt6" size="5" tabindex="7" value="0" /> <br /> <br /> Sitting quietly after lunch without alcohol <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt7" id="txt7" size="5" tabindex="8" value="0" /> <br /> <br /> In a car, while stopped for a few minutes in traffic <input onchange="handleChange(this);" onkeyup="updateTotal();" type="text" class="qi" name="txt8" id="txt8" size="5" tabindex="9" value="0" /> <br /> <br /> <div style="display: block clear:both;"> <div style="color: #00658f; font-size: 16px; float: left;"><strong>TOTAL SCORE</strong></div> </div> <div style="color: #00658f; font-size: 16px;"><strong><span name="span" id="total" style="float: right;"> </span></div> <div style="clear:both;"></div> <div class="form_element cf_button" style="float: right;"><input value="Calculate Score" name="submit" type="button" onclick="results();" /></div> </fieldset> </form> </div><!-- close quizcontainer --> <div id="result"> </div> </div><!-- close container --> </body> </html> Edited December 5, 2012 by ahaberman25 Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397745 Share on other sites More sharing options...
codefossa Posted December 5, 2012 Share Posted December 5, 2012 Then set total = 0; inside the function, but keep var total = 0; outside. Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397746 Share on other sites More sharing options...
ahaberman25 Posted December 5, 2012 Author Share Posted December 5, 2012 AWESOME Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397748 Share on other sites More sharing options...
ahaberman25 Posted December 5, 2012 Author Share Posted December 5, 2012 last question....is there an easy way to pass the var to a new window to display it using this function function results() { if ( total <= 6 ) { window.open('http://www.cre82morrow.com'); }else if ( total == 7 || total == 8 ) { window.open('http://www.apple.com'); }else { window.open('http://www.chrome.com'); } }; /* based on results opens a window */ each of the new windows are going to be local pages like results1.html Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397749 Share on other sites More sharing options...
codefossa Posted December 5, 2012 Share Posted December 5, 2012 Just add it to a $_GET variable. window.open('http://example.com/?total=' + total); Quote Link to comment https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/#findComment-1397753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.