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]()
Related
I'm using JQuery UI autocomplete to do a search in a database to search for a song. I have a global variable (artist) that I want to pass as a parameter to the autocomplete event.
var artist = 0;
// do some stuff to change the variable artist
$(function() {
$("#track1").autocomplete({
source: "tracks.php?id="+artist+"",
});
});
The problem is: the function is always called with the initial value (0) even when that value is changed. Is there an easy way to pass the changed global variable to the autocomplete function?
Thanks Carsten, the problem was indeed solved by moving $("#track1").autocomplete({into a function and calling it when the artist was updated.
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()
I need to run a function with a part dynamic variable in it's name.
The function to run:
$.element_gallery = function(state){}
The code that's calling it:
var name = "gallery";
window["$.element_"+name]('refresh');
This method seems to work with traditional Javascript functions but not with a jQuery function. Is there a way to run this without using eval() and without changing the function name?
Replace window with $
$["element_" + name]("refresh")
When you do window["$.foo"] it's looking for a property on the window object that's called $.foo. However, if you're using a jQuery function it's stored in the jQuery (or $) property of the window object, so the actual location of the function you want is window.$["foo"]. Change your code to:
var name = "gallery";
window.$["element_"+name]('refresh');
Or, since you can access global variables without specifying window., just:
var name = "gallery";
$[".element_"+name]('refresh');
I am using code lines like the following in order to fetch data from an intranet website:
util.setProp(obj, "firstNameOld", $(msg).find('#fname_a').text());
Now I have another function in the same file where I want to use the above again, resp. the value of that object - currently I am hard-coding this ('Test') for test purposes:
util.setProp(obj, "firstNameNew", 'Test');
How can I pass the value from the firstNameOld object in one function to the firstNameNew object in another function ? If a solution with global variables is better here than this would work as well.
Many thanks for any help with this, Tim.
I've never used the framework that includes util But I imagine that if there is a setProp() then there has to be a getProp() or something similar.
If so, you could do something like
util.setProp(obj, "firstNameNew", util.getProp(obj, "firstNameOld"));
This also relies on the assumption that you want to copy from two properties in the same object.
If not, then pass the desired source object in the getProp() call.
My guess is that functions (or properties) are called "firstNameOld" and "firstNameNew", so the first time you get it from selector, second time you want to do the same.
Try to use the local variable like that:
var text = $(msg).find('#fname_a').text();
//
util.setProp(obj, "firstNameOld", text);
//
util.setProp(obj, "firstNameNew", text);
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.