Jump to content

[SOLVED] Help with a Loop


Petty_Crim

Recommended Posts

I've got this loop which seems to behaving badly for some reason rather then going +1 it instantly jams the figure 10 into my function parameter.

 

I've placed a # in front of the the parameter: (position_count==9)

for (i=1; i<=position_count; i++)
	{
		text_button="position_button"+i;
		if (i==position_num)
		{
			document.getElementById("status"+position_num).innerHTML="<font color='red'><i><center>FREE</center></i></font>";
			document.getElementById(text_button).value='Apply';
			document.getElementById(text_button).onclick= function () {ajaxFunction(#i, 0, position_count)};
		}
		else
		{
			document.getElementById("i"+i).innerHTML=i;
			document.getElementById(text_button).value=i;
			document.getElementById(text_button).style.visibility='visible';
			document.getElementById(text_button).onclick= function () {ajaxFunction(#i, 0, position_count)};
		}
	}

It should output 1-9 in that parameter but it goes straight to 10.

Link to comment
https://forums.phpfreaks.com/topic/85493-solved-help-with-a-loop/
Share on other sites

Okay I solved it using this:

(function(i) {
        		var u = i;
       			document.getElementById(text_button).onclick= function() { 
				ajaxFunction(u, 0, position_count)};
  
     			})(i);

 

It seems when you place a function in a loop using the value thats getting incremented it waits and then inputs the last value of the number being incremented in this case 10.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.