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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.