javascript pass function name as parameter not working with jquery show hide











up vote
0
down vote

favorite












Here is a very very very simplified example.



I am trying to pass a function name "show" or "hide" as a parameter.



but it wont work , can you please help?



if(a==true){
showOrhide (hide);
}else{
showOrhide (show);
}

var showOrhide = function(doThis){
$("#myDiv").doThis();
};









share|improve this question
























  • possible duplicate of JavaScript object: access variable property by name as string
    – Felix Kling
    Aug 16 '13 at 12:41















up vote
0
down vote

favorite












Here is a very very very simplified example.



I am trying to pass a function name "show" or "hide" as a parameter.



but it wont work , can you please help?



if(a==true){
showOrhide (hide);
}else{
showOrhide (show);
}

var showOrhide = function(doThis){
$("#myDiv").doThis();
};









share|improve this question
























  • possible duplicate of JavaScript object: access variable property by name as string
    – Felix Kling
    Aug 16 '13 at 12:41













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Here is a very very very simplified example.



I am trying to pass a function name "show" or "hide" as a parameter.



but it wont work , can you please help?



if(a==true){
showOrhide (hide);
}else{
showOrhide (show);
}

var showOrhide = function(doThis){
$("#myDiv").doThis();
};









share|improve this question















Here is a very very very simplified example.



I am trying to pass a function name "show" or "hide" as a parameter.



but it wont work , can you please help?



if(a==true){
showOrhide (hide);
}else{
showOrhide (show);
}

var showOrhide = function(doThis){
$("#myDiv").doThis();
};






javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 16 '13 at 12:43









scunliffe

47.4k19104145




47.4k19104145










asked Aug 16 '13 at 12:39









Hello-World

3,1971661117




3,1971661117












  • possible duplicate of JavaScript object: access variable property by name as string
    – Felix Kling
    Aug 16 '13 at 12:41


















  • possible duplicate of JavaScript object: access variable property by name as string
    – Felix Kling
    Aug 16 '13 at 12:41
















possible duplicate of JavaScript object: access variable property by name as string
– Felix Kling
Aug 16 '13 at 12:41




possible duplicate of JavaScript object: access variable property by name as string
– Felix Kling
Aug 16 '13 at 12:41












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










It would work like this:



var showOrhide = function(doThis) {
$("#myDiv")[doThis]();
};

if(a) {
showOrhide('hide');
} else {
showOrhide('show');
}


Hoever, it's still extremely ugly. jQuery has a toggle() method that accepts a boolean argument which would be much more appropriate:



$("#myDiv").toggle(!a);





share|improve this answer





















  • +1 for the toggle solution. Much nicer than mine was.
    – Rory McCrossan
    Aug 16 '13 at 12:45










  • thanks for the solution - it has nothing to to with toggle but with the concept. thanks so much for your help
    – Hello-World
    Aug 16 '13 at 12:50


















up vote
0
down vote













If you want only pass show and hide functions you could pass only their names and call them in that way:



if(a==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}

function showOrhide (doThis){
$("#myDiv")[doThis]();
};





share|improve this answer























  • This will fail.
    – Neal
    Aug 16 '13 at 12:42










  • @Neal why do you think so?
    – codename-
    Aug 16 '13 at 12:42






  • 1




    Because showOrhide is undefined in the if statement.
    – Neal
    Aug 16 '13 at 12:43










  • This should work, because of hoisting: adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
    – codename-
    Aug 16 '13 at 12:44










  • @codename- Hoisting makes sure the variable is declared, but it doesn't hoist the definition (in case of the original answer which had the var showOrhide = ...)
    – JJJ
    Aug 16 '13 at 12:45




















up vote
-2
down vote













Disclaimer : Eval is evil (Quoting comments). Please use it on your own risk. Following answer is just for knowledge purpose. :-)



Another way, using eval.



var showOrhide = function(doThis){
eval("$('#myDiv')." + doThis + "()");
};

if(false==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}





share|improve this answer























  • eval is evil :P
    – codename-
    Aug 16 '13 at 12:46










  • Wow... No. This will fail...
    – Neal
    Aug 16 '13 at 12:46






  • 2




    And the prize for most unnecessary use of eval goes to...
    – JJJ
    Aug 16 '13 at 12:47






  • 1




    thanks I never new this - cool I learned something
    – Hello-World
    Aug 16 '13 at 12:56






  • 1




    "thanks I never new this - cool I learned something" --- and that's why, @Jithin, it's better not to talk about eval without heavy use of disclaimers because the actual use cases are so far and between that there's a good chance the OP will shoot themselves in the foot using it someplace else.
    – JJJ
    Aug 16 '13 at 13:17











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18273539%2fjavascript-pass-function-name-as-parameter-not-working-with-jquery-show-hide%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










It would work like this:



var showOrhide = function(doThis) {
$("#myDiv")[doThis]();
};

if(a) {
showOrhide('hide');
} else {
showOrhide('show');
}


Hoever, it's still extremely ugly. jQuery has a toggle() method that accepts a boolean argument which would be much more appropriate:



$("#myDiv").toggle(!a);





share|improve this answer





















  • +1 for the toggle solution. Much nicer than mine was.
    – Rory McCrossan
    Aug 16 '13 at 12:45










  • thanks for the solution - it has nothing to to with toggle but with the concept. thanks so much for your help
    – Hello-World
    Aug 16 '13 at 12:50















up vote
3
down vote



accepted










It would work like this:



var showOrhide = function(doThis) {
$("#myDiv")[doThis]();
};

if(a) {
showOrhide('hide');
} else {
showOrhide('show');
}


Hoever, it's still extremely ugly. jQuery has a toggle() method that accepts a boolean argument which would be much more appropriate:



$("#myDiv").toggle(!a);





share|improve this answer





















  • +1 for the toggle solution. Much nicer than mine was.
    – Rory McCrossan
    Aug 16 '13 at 12:45










  • thanks for the solution - it has nothing to to with toggle but with the concept. thanks so much for your help
    – Hello-World
    Aug 16 '13 at 12:50













up vote
3
down vote



accepted







up vote
3
down vote



accepted






It would work like this:



var showOrhide = function(doThis) {
$("#myDiv")[doThis]();
};

if(a) {
showOrhide('hide');
} else {
showOrhide('show');
}


Hoever, it's still extremely ugly. jQuery has a toggle() method that accepts a boolean argument which would be much more appropriate:



$("#myDiv").toggle(!a);





share|improve this answer












It would work like this:



var showOrhide = function(doThis) {
$("#myDiv")[doThis]();
};

if(a) {
showOrhide('hide');
} else {
showOrhide('show');
}


Hoever, it's still extremely ugly. jQuery has a toggle() method that accepts a boolean argument which would be much more appropriate:



$("#myDiv").toggle(!a);






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 16 '13 at 12:41









ThiefMaster

236k60459554




236k60459554












  • +1 for the toggle solution. Much nicer than mine was.
    – Rory McCrossan
    Aug 16 '13 at 12:45










  • thanks for the solution - it has nothing to to with toggle but with the concept. thanks so much for your help
    – Hello-World
    Aug 16 '13 at 12:50


















  • +1 for the toggle solution. Much nicer than mine was.
    – Rory McCrossan
    Aug 16 '13 at 12:45










  • thanks for the solution - it has nothing to to with toggle but with the concept. thanks so much for your help
    – Hello-World
    Aug 16 '13 at 12:50
















+1 for the toggle solution. Much nicer than mine was.
– Rory McCrossan
Aug 16 '13 at 12:45




+1 for the toggle solution. Much nicer than mine was.
– Rory McCrossan
Aug 16 '13 at 12:45












thanks for the solution - it has nothing to to with toggle but with the concept. thanks so much for your help
– Hello-World
Aug 16 '13 at 12:50




thanks for the solution - it has nothing to to with toggle but with the concept. thanks so much for your help
– Hello-World
Aug 16 '13 at 12:50












up vote
0
down vote













If you want only pass show and hide functions you could pass only their names and call them in that way:



if(a==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}

function showOrhide (doThis){
$("#myDiv")[doThis]();
};





share|improve this answer























  • This will fail.
    – Neal
    Aug 16 '13 at 12:42










  • @Neal why do you think so?
    – codename-
    Aug 16 '13 at 12:42






  • 1




    Because showOrhide is undefined in the if statement.
    – Neal
    Aug 16 '13 at 12:43










  • This should work, because of hoisting: adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
    – codename-
    Aug 16 '13 at 12:44










  • @codename- Hoisting makes sure the variable is declared, but it doesn't hoist the definition (in case of the original answer which had the var showOrhide = ...)
    – JJJ
    Aug 16 '13 at 12:45

















up vote
0
down vote













If you want only pass show and hide functions you could pass only their names and call them in that way:



if(a==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}

function showOrhide (doThis){
$("#myDiv")[doThis]();
};





share|improve this answer























  • This will fail.
    – Neal
    Aug 16 '13 at 12:42










  • @Neal why do you think so?
    – codename-
    Aug 16 '13 at 12:42






  • 1




    Because showOrhide is undefined in the if statement.
    – Neal
    Aug 16 '13 at 12:43










  • This should work, because of hoisting: adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
    – codename-
    Aug 16 '13 at 12:44










  • @codename- Hoisting makes sure the variable is declared, but it doesn't hoist the definition (in case of the original answer which had the var showOrhide = ...)
    – JJJ
    Aug 16 '13 at 12:45















up vote
0
down vote










up vote
0
down vote









If you want only pass show and hide functions you could pass only their names and call them in that way:



if(a==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}

function showOrhide (doThis){
$("#myDiv")[doThis]();
};





share|improve this answer














If you want only pass show and hide functions you could pass only their names and call them in that way:



if(a==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}

function showOrhide (doThis){
$("#myDiv")[doThis]();
};






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 16 '13 at 12:46









Neal

112k30209271




112k30209271










answered Aug 16 '13 at 12:41









codename-

8,19722029




8,19722029












  • This will fail.
    – Neal
    Aug 16 '13 at 12:42










  • @Neal why do you think so?
    – codename-
    Aug 16 '13 at 12:42






  • 1




    Because showOrhide is undefined in the if statement.
    – Neal
    Aug 16 '13 at 12:43










  • This should work, because of hoisting: adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
    – codename-
    Aug 16 '13 at 12:44










  • @codename- Hoisting makes sure the variable is declared, but it doesn't hoist the definition (in case of the original answer which had the var showOrhide = ...)
    – JJJ
    Aug 16 '13 at 12:45




















  • This will fail.
    – Neal
    Aug 16 '13 at 12:42










  • @Neal why do you think so?
    – codename-
    Aug 16 '13 at 12:42






  • 1




    Because showOrhide is undefined in the if statement.
    – Neal
    Aug 16 '13 at 12:43










  • This should work, because of hoisting: adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
    – codename-
    Aug 16 '13 at 12:44










  • @codename- Hoisting makes sure the variable is declared, but it doesn't hoist the definition (in case of the original answer which had the var showOrhide = ...)
    – JJJ
    Aug 16 '13 at 12:45


















This will fail.
– Neal
Aug 16 '13 at 12:42




This will fail.
– Neal
Aug 16 '13 at 12:42












@Neal why do you think so?
– codename-
Aug 16 '13 at 12:42




@Neal why do you think so?
– codename-
Aug 16 '13 at 12:42




1




1




Because showOrhide is undefined in the if statement.
– Neal
Aug 16 '13 at 12:43




Because showOrhide is undefined in the if statement.
– Neal
Aug 16 '13 at 12:43












This should work, because of hoisting: adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
– codename-
Aug 16 '13 at 12:44




This should work, because of hoisting: adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
– codename-
Aug 16 '13 at 12:44












@codename- Hoisting makes sure the variable is declared, but it doesn't hoist the definition (in case of the original answer which had the var showOrhide = ...)
– JJJ
Aug 16 '13 at 12:45






@codename- Hoisting makes sure the variable is declared, but it doesn't hoist the definition (in case of the original answer which had the var showOrhide = ...)
– JJJ
Aug 16 '13 at 12:45












up vote
-2
down vote













Disclaimer : Eval is evil (Quoting comments). Please use it on your own risk. Following answer is just for knowledge purpose. :-)



Another way, using eval.



var showOrhide = function(doThis){
eval("$('#myDiv')." + doThis + "()");
};

if(false==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}





share|improve this answer























  • eval is evil :P
    – codename-
    Aug 16 '13 at 12:46










  • Wow... No. This will fail...
    – Neal
    Aug 16 '13 at 12:46






  • 2




    And the prize for most unnecessary use of eval goes to...
    – JJJ
    Aug 16 '13 at 12:47






  • 1




    thanks I never new this - cool I learned something
    – Hello-World
    Aug 16 '13 at 12:56






  • 1




    "thanks I never new this - cool I learned something" --- and that's why, @Jithin, it's better not to talk about eval without heavy use of disclaimers because the actual use cases are so far and between that there's a good chance the OP will shoot themselves in the foot using it someplace else.
    – JJJ
    Aug 16 '13 at 13:17















up vote
-2
down vote













Disclaimer : Eval is evil (Quoting comments). Please use it on your own risk. Following answer is just for knowledge purpose. :-)



Another way, using eval.



var showOrhide = function(doThis){
eval("$('#myDiv')." + doThis + "()");
};

if(false==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}





share|improve this answer























  • eval is evil :P
    – codename-
    Aug 16 '13 at 12:46










  • Wow... No. This will fail...
    – Neal
    Aug 16 '13 at 12:46






  • 2




    And the prize for most unnecessary use of eval goes to...
    – JJJ
    Aug 16 '13 at 12:47






  • 1




    thanks I never new this - cool I learned something
    – Hello-World
    Aug 16 '13 at 12:56






  • 1




    "thanks I never new this - cool I learned something" --- and that's why, @Jithin, it's better not to talk about eval without heavy use of disclaimers because the actual use cases are so far and between that there's a good chance the OP will shoot themselves in the foot using it someplace else.
    – JJJ
    Aug 16 '13 at 13:17













up vote
-2
down vote










up vote
-2
down vote









Disclaimer : Eval is evil (Quoting comments). Please use it on your own risk. Following answer is just for knowledge purpose. :-)



Another way, using eval.



var showOrhide = function(doThis){
eval("$('#myDiv')." + doThis + "()");
};

if(false==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}





share|improve this answer














Disclaimer : Eval is evil (Quoting comments). Please use it on your own risk. Following answer is just for knowledge purpose. :-)



Another way, using eval.



var showOrhide = function(doThis){
eval("$('#myDiv')." + doThis + "()");
};

if(false==true){
showOrhide ('hide');
}else{
showOrhide ('show');
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 5:06

























answered Aug 16 '13 at 12:46









Jithin

2,09311438




2,09311438












  • eval is evil :P
    – codename-
    Aug 16 '13 at 12:46










  • Wow... No. This will fail...
    – Neal
    Aug 16 '13 at 12:46






  • 2




    And the prize for most unnecessary use of eval goes to...
    – JJJ
    Aug 16 '13 at 12:47






  • 1




    thanks I never new this - cool I learned something
    – Hello-World
    Aug 16 '13 at 12:56






  • 1




    "thanks I never new this - cool I learned something" --- and that's why, @Jithin, it's better not to talk about eval without heavy use of disclaimers because the actual use cases are so far and between that there's a good chance the OP will shoot themselves in the foot using it someplace else.
    – JJJ
    Aug 16 '13 at 13:17


















  • eval is evil :P
    – codename-
    Aug 16 '13 at 12:46










  • Wow... No. This will fail...
    – Neal
    Aug 16 '13 at 12:46






  • 2




    And the prize for most unnecessary use of eval goes to...
    – JJJ
    Aug 16 '13 at 12:47






  • 1




    thanks I never new this - cool I learned something
    – Hello-World
    Aug 16 '13 at 12:56






  • 1




    "thanks I never new this - cool I learned something" --- and that's why, @Jithin, it's better not to talk about eval without heavy use of disclaimers because the actual use cases are so far and between that there's a good chance the OP will shoot themselves in the foot using it someplace else.
    – JJJ
    Aug 16 '13 at 13:17
















eval is evil :P
– codename-
Aug 16 '13 at 12:46




eval is evil :P
– codename-
Aug 16 '13 at 12:46












Wow... No. This will fail...
– Neal
Aug 16 '13 at 12:46




Wow... No. This will fail...
– Neal
Aug 16 '13 at 12:46




2




2




And the prize for most unnecessary use of eval goes to...
– JJJ
Aug 16 '13 at 12:47




And the prize for most unnecessary use of eval goes to...
– JJJ
Aug 16 '13 at 12:47




1




1




thanks I never new this - cool I learned something
– Hello-World
Aug 16 '13 at 12:56




thanks I never new this - cool I learned something
– Hello-World
Aug 16 '13 at 12:56




1




1




"thanks I never new this - cool I learned something" --- and that's why, @Jithin, it's better not to talk about eval without heavy use of disclaimers because the actual use cases are so far and between that there's a good chance the OP will shoot themselves in the foot using it someplace else.
– JJJ
Aug 16 '13 at 13:17




"thanks I never new this - cool I learned something" --- and that's why, @Jithin, it's better not to talk about eval without heavy use of disclaimers because the actual use cases are so far and between that there's a good chance the OP will shoot themselves in the foot using it someplace else.
– JJJ
Aug 16 '13 at 13:17


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18273539%2fjavascript-pass-function-name-as-parameter-not-working-with-jquery-show-hide%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Feedback on college project

Futebolista

Albești (Vaslui)