Search
Sponsor
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Codes Author
Fri, 28-Nov-2014 Authors
Vishal@Neeraj
240
Sheonarayan
40
All Time Authors
Satyapriyanayak
12160
Vishal@Neeraj
10880
Jayakumars
8120

Latest members | More ...


(Statistics delayed by 5 minutes)
 Codes

Explain with a code sample how to pass a method in javascript as an argument to another javascript method?

Tripati_tutu
Posted by Tripati_tutu under JavaScript on 12/8/2010 2:43:29 AM | Points: 40 | Views : 1906 | Status : [Member]

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title></title>
<script type="text/javascript">
function Sample(a)
{
var message;
if (a == 1)
{
message = "First function is executed";
ExecuteFunction(Function1, message)
}
if (a == 2)
{
message = "Second function is executed";
ExecuteFunction(Function2, message)
}
}

//Executes the Function that is passed as parameter
function ExecuteFunction(funObj, params)
{
funObj(params);
}
function Function1(message)
{
alert("Function1: " + message);
}
function Function2(message)
{
alert("Function2: " + message);
}
</script>
</head>
<body>
<input type = "button" onclick = "Sample(1)" value = "For Fun1" />
<input type = "button" onclick = "Sample(2)" value = "For Fun2" />
</body>
</html>


Here I have created two buttons. So accordingly when you will click on the buttons, the corresponding function will automatically execute.


Found interesting? Add this to:



Comments or Responses

Posted by: Kishork80 on: 12/8/2010 | Level:Starter | Status: [Member] | Points: 10

intresting :)


Login to post response