Calling a Function stored in Variable from jQuery - javascript

I am trying to call a function typed variable from jQuery code. But don't know how to do that. eval() takes string input but I have function typed variable. I have tried the following code
if($.isFunction(successCallback)){
window[successCallback]();
}

Calling like a normal function worked.
successCallback()

Related

Is data a reserved variable in getJSON() callback function?

Here is the descrition of getJSON in jquery:
$(selector).getJSON(url,data,success(data,status,xhr))
Here success(data,status,xhr) is a callback function when success. I often see the code below:
$(select(.getJSON('my/url', function(data) {....});
Here variable data holds the data returned by the http server. My question is that instead of using name data here, can I use variable name like server_data as below?
$(select(.getJSON('my/url', function(server_data) {....});
It's a function argument name. You can name it whatever you want, so long as it follows the valid javascript variable name format.
Ref: What characters are valid for JavaScript variable names?
Edit: Also, easy enough to test without asking a question.

how to call a function using dynamic function name (parameterized) in nodeJS

I am able to call function in my code.. however I want to make my calling function name as variable so that it can call any function based on my variable value ..
reportPage.accounts()['catType']()
so here I want to make catType as variable, so that I can pass any value.. How to declare/call here..
You replace the string literal with the variable, exactly as you would anywhere else.
var thing = 'catType';
reportPage.accounts()[thing]()

JavaScript: Sending comma separated string of parameters to a javascript function

I am having a function in javascript as
function add(v1,v2){
var add=v1+v2;
}
Now I am calling this function as below -
write.out(var param="1,2";);
write.out(window[add](param););
Using the above call, it's not working. What it does is it gives the complete string "1,2" as value to the first param(v1) of the function.
Its working if I call the function in following way -
write.out(var param1="1";);
write.out(var param2="2";);
write.out(window[add](param1,param2););
I want to achieve it using the first way where i can send the parameters as a comma separated string of parameters.
Can some one help me out how this can be done...
Thanks!!!
You can make usage of ECMAscripts .apply(), which calls a function and accepts an array of paramters.
window['add'].apply(null, param.split(','));
That way, we execute the add function, setting its context to null (you could also change that if you need) and pass in the two paramters. Since we need an Array, we call split() on the string before.
So basically, the above line is the same as
add(1,2);
Since you're haveing that function in the global context (window), we don't even need to write it that explicitly.
add.apply(null, param.split(','));
will just be fine.

javascript: assign function parameter if functioncall is a string?

Following example - the YoutubePlayer API
videoID.addEventListener('onStateChange', 'foo');
In the documentation they say, video.addEventListener(string: event, string: function).
That means the first parameter is the event (in my case onStateChange) and the second parameter is the function that is getting called when the even is triggered.
This youtube sample is just a good example, I've alredy had this question a few times before.
If the function to call is passed as a string, is there any chance to assign a a parameter to that function?
Imagine the function I want to call looks like this.
function foo(something) {
console.log(something).
}
It's obviously not possible to add a parameter to the function call is it? Likeā€¦
videoID.addEventListener('onStateChange', 'foo(videoID)');
Thank you for your information and answers.
You could do something like:
videoID.addEventListener('onStateChange', function()
{
foo(videoID);
});
...if I'm understanding you correctly.

jQuery javascript scoping problem

I have a hidden input element that I am using as a counter to use to name more input elements generated by JavaScript. To get the value of the counter i use
parseInt($('#counter').val());
However I use this code snippet several times in my code, so I thought it would be good to put it in a function
function getCounter(){
parseInt($('#counter').val());
}
this always returns undefined, while running just the code snippet returns the correct value. This happens in several ways that I tried defeining the function, as a function inside a $(function(){}), as a global function etc. How do I fix the scoping?
Add "return" to your return statement :)
function getCounter(){
return parseInt($('#counter').val());
}
How about adding a return
function getCounter(){
return parseInt($('#counter').val());
}

Categories