Solved

How to dynamically assign function arguments in for loop

Posted on 2008-10-25
3
661 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
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

821 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