Skip to content Skip to sidebar Skip to footer

Call A Javascript Function Using String Name

It is a bit complicated for to explain properly what I mean, so please try to understand me :) I want to be able in javascript to call a element method/function using text sent as

Solution 1:

Use bracket notation;

document.getElementById('some_id')[function_name]();

Solution 2:

call function name using string :

var myFunc = window[myFuncName];

you can sent parameter like this :

<buttononclick="myFunction('Harry Potter','Wizard')">Try it</button><script>functionmyFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
}
</script>

Post a Comment for "Call A Javascript Function Using String Name"