473,411 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

function to execute all functions listed in an array

I am trying to create a function that executes all the functions listed
in an array. I cannot get it to work.

<html>
<body>
<script language="JavaScript">

var length_of_array = 2;
var function_array = new Array(
"no1()",
"no2()",
length_of_array);

function main_function(cat, cow)
{
for(var x = 0; x<cow; x++)
{
cat[x];
}
}

function no1()
{
alert("hello");
}

function no2()
{
alert("good bye");
}

</script>
<input type="button" value="click"
onClick="main_function(function_array, length_of_array)">
</body>
</html>

Your help will be appreciated.

Bundy
Aug 24 '06 #1
4 2101


Bundy wrote:
I am trying to create a function that executes all the functions listed
in an array. I cannot get it to work.
var function_array = new Array(
"no1()",
"no2()",
You have strings in that array, not functions. If you want to have
functions in the array then simply like this
var function_array = [no1, no2];
or
var function_array = new Array(no1, no2);
function no1()
{
alert("hello");
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 24 '06 #2
Bundy <bi*******************@vfemail.netwrites:
I am trying to create a function that executes all the functions
listed in an array.
<script language="JavaScript">
(invalid HTML 4, should be, e.g., <script type="text/javascript">)
var length_of_array = 2;
var function_array = new Array(
"no1()",
"no2()",
length_of_array);
Why store code in the array, and not the functions themselves?
And why store the length of the array? Arrays are quite capable
of knowing their own length.

I suggest doing:
---
var function_array = new Array(no1, no2);

function main_function(cat, cow) {
for(var x = 0; x < cat.length; x++) {
(cat[x])();
}
}
....
onclick="main_function(function_array);"
---

Using references to the functions themselves obviously requires them
to be defined at the point the array is created. That means that the
functions should be defined in the same script element as the array,
or in previously executed script element.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 24 '06 #3
var a=function(){alert("a")}
,b=function(){alert("b")}
,c=function(){alert("c")}
,d=function(){alert("d")}
,arr=[a,b,c,d]

for(var i in arr){
arr[i]()
}

*** Sent via Developersdex http://www.developersdex.com ***
Aug 24 '06 #4
Thank you to you both.

Bundy
Aug 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Raphael Bauduin | last post by:
Hi, I'm looking at the logging of a database we'll put in production soon. I've seen some posts on this list about history tables, like mentioned in...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.