I have a variable name that I pass into a plugin, but the variable is actually a function. I use jquery $.isFunction to check if it is a function, and if it is, it should execute the function.
But I can't seem to make it work, I put some examples in jsfiddle:/
But here is a sample code:
HTML
<a class="one" href="#">click</a><br />
<a class="two" href="#">click</a><br />
<a class="three" href="#">click</a><br />
JS
$(document).ready(function() {
help = function(var1) {
alert(var1);
}
function help2(var1) {
alert(var1);
}
$('a.one').click(function() {
var functionName = "help";
if ($.isFunction([functionName])) {[functionName]("hello");
} else {
alert("not a function");
}
return false;
});
$('a.two').click(function() {
var functionName = "help";
if ($.isFunction(functionName)) {
functionName("hello");
} else {
alert("not a function");
}
return false;
});
$('a.three').click(function() {
var functionName = "help2";
if ($.isFunction(functionName)) {
functionName("hello");
} else {
alert("not a function");
}
return false;
});
$('a.four').click(function() {
var functionName = "help2";
if ($.isFunction([functionName])) {[functionName]("hello");
} else {
alert("not a function");
}
return false;
});
});
As you can see, I tired a bunch of things, but all the wrong ones probably... I inspired some of them from: jQuery - use variable as function name
Overall I'm passing a variable that has the same name as a function, using jquery to check if it is a function, if it is, it should execute the function.
Thanks in advance for your help.
I have a variable name that I pass into a plugin, but the variable is actually a function. I use jquery $.isFunction to check if it is a function, and if it is, it should execute the function.
But I can't seem to make it work, I put some examples in jsfiddle:http://jsfiddle/tZ6U9/8/
But here is a sample code:
HTML
<a class="one" href="#">click</a><br />
<a class="two" href="#">click</a><br />
<a class="three" href="#">click</a><br />
JS
$(document).ready(function() {
help = function(var1) {
alert(var1);
}
function help2(var1) {
alert(var1);
}
$('a.one').click(function() {
var functionName = "help";
if ($.isFunction([functionName])) {[functionName]("hello");
} else {
alert("not a function");
}
return false;
});
$('a.two').click(function() {
var functionName = "help";
if ($.isFunction(functionName)) {
functionName("hello");
} else {
alert("not a function");
}
return false;
});
$('a.three').click(function() {
var functionName = "help2";
if ($.isFunction(functionName)) {
functionName("hello");
} else {
alert("not a function");
}
return false;
});
$('a.four').click(function() {
var functionName = "help2";
if ($.isFunction([functionName])) {[functionName]("hello");
} else {
alert("not a function");
}
return false;
});
});
As you can see, I tired a bunch of things, but all the wrong ones probably... I inspired some of them from: jQuery - use variable as function name
Overall I'm passing a variable that has the same name as a function, using jquery to check if it is a function, if it is, it should execute the function.
Thanks in advance for your help.
Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Sep 29, 2012 at 19:13 denislexicdenislexic 11.4k26 gold badges88 silver badges137 bronze badges 7-
4
[functionName]
is always an array, not a function. AndfunctionName
is also not a function, it's a string.$.isFunction
does not test whether a function with such a name exists, it really tests whether the value you pass to it is a function. The way you set up your code you cannot actually access those two functions by a string containing their names. – Felix Kling Commented Sep 29, 2012 at 19:17 - Ok, that makes sense. Thanks (I do get confused with this). Is there any solution? – denislexic Commented Sep 29, 2012 at 19:19
-
Is this just a simplified example? Because here you could just call
help
andhelp2
directly. You mentioned a plugin, so whether a solution exist might depend on how the plugin works. – Felix Kling Commented Sep 29, 2012 at 19:21 -
@FelixKling - Let's hope that
help = function...
meant to bevar help
, because you can call it: jsfiddle/userdude/jSvPu Probably not remended... – Jared Farrish Commented Sep 29, 2012 at 19:23 -
1
@JaredFarrish: Yeah, I assumed that actually. But even if not,
help2
is local, so I am still at least 50% right ;) :D – Felix Kling Commented Sep 29, 2012 at 19:24
3 Answers
Reset to default 15If you are wanting to call a function by a string of its name just use window
.
var functionName = "help";
if ($.isFunction(window[functionName])) {
window[functionName]("hello");
} else {
alert("not a function");
}
You can use the following to invoke functions that are defined in the window/global scope, such as the function help
:
if ($.isFunction(window[functionName])) {
window[functionName]("hello");
}
help2
, on the other hand, is not accessible this way since you are defining it in a closure. A possibile solution is to define the function outside of the .ready()
handler. Then, you can use window[functionName]
to call it:
var namespace = {
help: function (var1) {
alert(var1);
},
help2: function (var1) {
alert(var1);
}
}
$(document).ready(function() {
var functionName = "help";
if ($.isFunction(namespace[functionName])) {
namespace[functionName]("hello");
}
});
DEMO.
Check Fiddle for the working example.
Example have only one link working. make other links similarly.
Edit: after first ment
HTML
<a class="one" href="#">click</a><br />
JS
var help = function(var1) {
alert(var1);
}
$(document).ready(function() {
$('a.one').click(function() {
var functionName = help;
if ($.isFunction(functionName)) {
functionName('test');
} else {
alert("not a function");
}
return false;
});
});