Jump to content

Recommended Posts

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()

Link to comment
https://forums.phpfreaks.com/topic/271619-grab-a-var-from-another-function/
Share on other sites

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 by requinix

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>

post-134159-0-69618700-1354740902_thumb.jpg

Edited by ahaberman25

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.