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();
};
javascript jquery
add a comment |
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();
};
javascript jquery
possible duplicate of JavaScript object: access variable property by name as string
– Felix Kling
Aug 16 '13 at 12:41
add a comment |
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();
};
javascript jquery
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
javascript jquery
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
add a comment |
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
add a comment |
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);
+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
add a comment |
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]();
};
This will fail.
– Neal
Aug 16 '13 at 12:42
@Neal why do you think so?
– codename-
Aug 16 '13 at 12:42
1
BecauseshowOrhideisundefinedin 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 thevar showOrhide = ...)
– JJJ
Aug 16 '13 at 12:45
|
show 1 more comment
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');
}
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
|
show 1 more comment
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);
+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
add a comment |
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);
+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
add a comment |
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);
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);
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
add a comment |
+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
add a comment |
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]();
};
This will fail.
– Neal
Aug 16 '13 at 12:42
@Neal why do you think so?
– codename-
Aug 16 '13 at 12:42
1
BecauseshowOrhideisundefinedin 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 thevar showOrhide = ...)
– JJJ
Aug 16 '13 at 12:45
|
show 1 more comment
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]();
};
This will fail.
– Neal
Aug 16 '13 at 12:42
@Neal why do you think so?
– codename-
Aug 16 '13 at 12:42
1
BecauseshowOrhideisundefinedin 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 thevar showOrhide = ...)
– JJJ
Aug 16 '13 at 12:45
|
show 1 more comment
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]();
};
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]();
};
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
BecauseshowOrhideisundefinedin 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 thevar showOrhide = ...)
– JJJ
Aug 16 '13 at 12:45
|
show 1 more comment
This will fail.
– Neal
Aug 16 '13 at 12:42
@Neal why do you think so?
– codename-
Aug 16 '13 at 12:42
1
BecauseshowOrhideisundefinedin 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 thevar 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
|
show 1 more comment
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');
}
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
|
show 1 more comment
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');
}
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
|
show 1 more comment
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');
}
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');
}
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
|
show 1 more comment
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
|
show 1 more 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.
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.
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%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
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
possible duplicate of JavaScript object: access variable property by name as string
– Felix Kling
Aug 16 '13 at 12:41