Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using variable to call function

Status
Not open for further replies.

sezme

Programmer
Oct 31, 2003
26
GB


Not sure if this is possible

function test(){
alert("It works")
}

i want to be able to store the name in a variable

var temporary = test

and be able to call the function

document. blah blah blah

can I do this - does anyone have any syntax

Thanks in advance




 
store the function name in a string and then execute it using the eval() function


Code:
<HTML>
<HEAD>
<TITLE>Test</TITLE>
<SCRIPT LANGUAGE=JavaScript>

var blah = &quot;test()&quot;;

function test() {
   alert (&quot;blah&quot;);
}

</SCRIPT>
<BODY>
<FORM NAME=&quot;blahForm&quot;>
<INPUT TYPE=BUTTON VALUE=BLAH ONCLICK='javascript:eval(blah);'
</FORM>
</BODY>
</HTML>

-kaht
 
I forgot to close my button tag above.... not that it probably matters for your program :)
Code:
<INPUT TYPE=BUTTON VALUE=BLAH ONCLICK='javascript:eval(blah);'>

-kaht
 


I was just about to thank you then - but as you didn't close the tag i wont bother :mad:

oh go on then

thanks
 
you can also do something like this:
Code:
function foo() {
	alert(&quot;foo&quot;);
}

var bar = foo;

bar();
bar();
bar();

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top