Solved

How to dynamically assign function arguments in for loop

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now