Solved

How to dynamically assign function arguments in for loop

Posted on 2008-10-25
3
666 Views
Last Modified: 2012-05-05
Hi Experts,

I have a series of buttons and want to dynamically assign their onclick functionality in a for loop within java script.  The onclick event should simply call a function.  I would like the index of the loop to be used as an argument to the function.  

The behavior I'm noticing is that the argument is the same for all buttons.  How do I assign them a unique value?  I feel like this is some java script variable scope issue.  My understanding is that there isn't scope in java script, which I think is contributing to the problem.

Please see the attached code snippet.  This is a simple html page that exhibits the problem behavior.  I understand it will run in IE and not firefox, but that is okay as the application I'm working on only uses IE.  I have even tried using an array to define a new Number object for each iteration of the loop, but this gives me undefined arguments.

Any advice, insight, or improvements are appreciated!  

I've been struggling with this for much longer than I should have now so I figured I would ask some other experts.  

Thanks!


<html>
<head>
 
<script FOR=window EVENT=onload LANGUAGE="javascript">
 
for(var i = 1; i < 5; i++)
{
var test = i;
var oButton = document.getElementById("test"+i);
oButton.onclick = function(){testFunction(test);};
}
 
</script>
 
<script type="text/javascript">
function testFunction(iNum)
{
alert(iNum);
}
</script>
 
 
 
</head>
<body>
 
<form>
<input id="test1" type="button" 
value="test1">
<input id="test2" type="button" 
value="test2">
<input id="test3" type="button" 
value="test3">
<input id="test4" type="button" 
value="test4">
 
</form>
 
</body>
</html>

Open in new window

0
Comment
Question by:Repus024
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
Repus024 earned 0 total points
ID: 22804883
A colleague of mine discovered a solution to this issue.  The solution is to create a property on the button object to store the argument.  Please see the attached code snippet for the solution in action
<html>
<head>
 
<script FOR=window EVENT=onload LANGUAGE="javascript">
 
for(var i = 1; i < 5; i++)
{
var oButton = document.getElementById("test"+i);
oButton.number = i;
oButton.onclick = function(){testFunction(this.number);};
}
 
</script>
 
<script type="text/javascript">
function testFunction(iNum)
{
alert(iNum);
}
</script>
 
 
 
</head>
<body>
 
<form>
<input id="test1" type="button" 
value="test1">
<input id="test2" type="button" 
value="test2">
<input id="test3" type="button" 
value="test3">
<input id="test4" type="button" 
value="test4">
 
</form>
 
</body>
</html>

Open in new window

0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 22804906
Is it the number that want to pass to the function?
If so, this number is also in the id attribute of the element ie test1.

You could also assign a custom attribute to the input element and use that. (see code)
<script FOR=window EVENT=onload LANGUAGE="javascript">
 
for(var i = 1; i < 5; i++)
{
var test = i;
var oButton = document.getElementById("test"+i);
oButton.CustomValue = someValue;
oButton.onclick = function(){testFunction(this);};
}
 
function testFunction(el)
{
alert(el.CustomValue);
}
 
</script>

Open in new window

0
 
LVL 3

Author Comment

by:Repus024
ID: 22804912
The custom attribute is exactly the solution I used.  Please see my comment on the original question.

Thanks for your solution.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question