Appear in Online Interview to test your skills and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Submit content and win cash & prizes monthly.
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Codes Author
Thu, 24-Apr-2014 Authors
All Time Authors
Satyapriyanayak
11600
Jayakumars
8120
Niladri.biswas
6000

Latest members | More ...


(Statistics delayed by 5 minutes)

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 : 1704 | 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.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.