PDA

View Full Version : Dynamic parameters for dynamic onclicks?


Dylan Leblanc
05-29-2003, 11:00 PM
I have an array (called myArray in the example) which contains information about all the checkboxes and such on the search form.

I am looping through the array and setting the onclick for each checkbox. Here is a way to do this: document.search.status[0].onclick = function() { StatusChecked('all', 0) }

But my problem is that the 0 that is being passed to the StatusChecked() function needs to actually be a dynamic value. Each time through the loop that value needs to be changed. But as far as I can tell, if I substitute it for a variable, it doesn't work. i understand why it doesn't work this way, but can't figure out an alternative way that does work.

You can see in the second line is StatusChecked('all', index), but that is not going to work. What can I do?

for (index = 0; index < myArray.length; index++) {

document.search[myArray[index]['name'] + 'All'].onclick = function() { StatusChecked('all', index); }

for (i = 0; i < document.search[myArray[index]['name'] + '[]'].length; i ++) {
document.search[myArray[index]['name'] + '[]'][i ].onclick = function() { StatusChecked(this, i); }
}
}

I thought maybe something like onclick = function(var i = index) { StatusChecked('all', i); } might do the trick, but no.

beetle
05-29-2003, 11:21 PM
Before I even consider a solution - let me ask this:

Is it absolutely imperative that the StatusChecked function receives the index?

Can I see all your code, or a link?

Dylan Leblanc
05-29-2003, 11:41 PM
The reason the index number from the loop needs to be passed to the checkbox's onclick, is because this is the index number of that checkbox in the array I have setup.

When the onclick function is called, the StatusChecked() function will know where to get information about that checkbox in the all array (see source code), by using the index number which was passed in.

You can see all the code on this page (the JS is at the bottom): http://skyscraperpage.com/temp/form.htm

I have changed it so that 0 is being passed because I don't know what else to do at this point. 0 in the all array contains information for the status checkboxes. So the status checkboxes on that page work. You can try them. But none of the other checkboxes will work yet.