Petty_Crim Posted January 11, 2008 Share Posted January 11, 2008 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 More sharing options...
Petty_Crim Posted January 11, 2008 Author Share Posted January 11, 2008 Anyone know? My loop is outputting 1-9 for the button names, but for some reason when it comes to the function it just slams in a big 10. Link to comment https://forums.phpfreaks.com/topic/85493-solved-help-with-a-loop/#findComment-436396 Share on other sites More sharing options...
Petty_Crim Posted January 11, 2008 Author Share Posted January 11, 2008 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 https://forums.phpfreaks.com/topic/85493-solved-help-with-a-loop/#findComment-436584 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.