Toggle variable through a function
this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.
Here is it:
var hello = false
function toggleSt(I, E)
if ((I == "activate") && (!E))
E = !E
alert("activated")
else if ((I == "disable") && (E))
E = !E
alert("disabled")
toggleSt("activate", hello)
alert(hello)
I pasted the code on JSFiddle,
http://jsfiddle.net/kpDSr/
Hello is still false.
javascript variables toggle
add a comment |
this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.
Here is it:
var hello = false
function toggleSt(I, E)
if ((I == "activate") && (!E))
E = !E
alert("activated")
else if ((I == "disable") && (E))
E = !E
alert("disabled")
toggleSt("activate", hello)
alert(hello)
I pasted the code on JSFiddle,
http://jsfiddle.net/kpDSr/
Hello is still false.
javascript variables toggle
1
Ewill not be a reference tohello, it will only have the same value. ChangingEwill not changehello.
– Felix Kling
Aug 10 '11 at 15:34
add a comment |
this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.
Here is it:
var hello = false
function toggleSt(I, E)
if ((I == "activate") && (!E))
E = !E
alert("activated")
else if ((I == "disable") && (E))
E = !E
alert("disabled")
toggleSt("activate", hello)
alert(hello)
I pasted the code on JSFiddle,
http://jsfiddle.net/kpDSr/
Hello is still false.
javascript variables toggle
this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.
Here is it:
var hello = false
function toggleSt(I, E)
if ((I == "activate") && (!E))
E = !E
alert("activated")
else if ((I == "disable") && (E))
E = !E
alert("disabled")
toggleSt("activate", hello)
alert(hello)
I pasted the code on JSFiddle,
http://jsfiddle.net/kpDSr/
Hello is still false.
javascript variables toggle
javascript variables toggle
edited Nov 13 '18 at 9:19
Cœur
17.5k9104145
17.5k9104145
asked Aug 10 '11 at 15:31
ImplosionsImplosions
1114
1114
1
Ewill not be a reference tohello, it will only have the same value. ChangingEwill not changehello.
– Felix Kling
Aug 10 '11 at 15:34
add a comment |
1
Ewill not be a reference tohello, it will only have the same value. ChangingEwill not changehello.
– Felix Kling
Aug 10 '11 at 15:34
1
1
E will not be a reference to hello, it will only have the same value. Changing E will not change hello.– Felix Kling
Aug 10 '11 at 15:34
E will not be a reference to hello, it will only have the same value. Changing E will not change hello.– Felix Kling
Aug 10 '11 at 15:34
add a comment |
2 Answers
2
active
oldest
votes
Felix is right. Try:
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello;
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate");
alert(hello)
But that's hardcoding! Any possible alternative?
– Implosions
Aug 10 '11 at 15:47
add a comment |
You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate")
alert(hello)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7013488%2ftoggle-variable-through-a-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Felix is right. Try:
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello;
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate");
alert(hello)
But that's hardcoding! Any possible alternative?
– Implosions
Aug 10 '11 at 15:47
add a comment |
Felix is right. Try:
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello;
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate");
alert(hello)
But that's hardcoding! Any possible alternative?
– Implosions
Aug 10 '11 at 15:47
add a comment |
Felix is right. Try:
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello;
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate");
alert(hello)
Felix is right. Try:
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello;
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate");
alert(hello)
edited Aug 10 '11 at 15:45
answered Aug 10 '11 at 15:38
Gabriel RossGabriel Ross
4,03112127
4,03112127
But that's hardcoding! Any possible alternative?
– Implosions
Aug 10 '11 at 15:47
add a comment |
But that's hardcoding! Any possible alternative?
– Implosions
Aug 10 '11 at 15:47
But that's hardcoding! Any possible alternative?
– Implosions
Aug 10 '11 at 15:47
But that's hardcoding! Any possible alternative?
– Implosions
Aug 10 '11 at 15:47
add a comment |
You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate")
alert(hello)
add a comment |
You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate")
alert(hello)
add a comment |
You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate")
alert(hello)
You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.
var hello = false
function toggleSt(I)
if ((I == "activate") && (!hello))
hello = !hello
alert("activated")
else if ((I == "disable") && (hello))
hello = !hello
alert("disabled")
toggleSt("activate")
alert(hello)
answered Aug 10 '11 at 15:46
Andreas KöberleAndreas Köberle
47.6k39177235
47.6k39177235
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7013488%2ftoggle-variable-through-a-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Ewill not be a reference tohello, it will only have the same value. ChangingEwill not changehello.– Felix Kling
Aug 10 '11 at 15:34