hi , My requirement is to have an infinite loop where after each click i should get next element.And after the last element i should get the first element. <script language="JavaScript"> function ChangeAll(f,buton,frm) { var mySplitResult=f.split(","); for(i = 0; i < mySplitResult.length; i++) { if(buton == mySplitResult[i]) { i++; { frm.value = mySplitResult[i]; if(i<mySplitResult-1) { i=0; } } } </script> <% DIM Definition(4) Dim d d="S,F,G,T" response.write("<form method='post'>") response.write("<input type='button' value='S' onClick='ChangeAll("""&d&""",this.value,this)' ") response.write("</form>") %> Code (markup): This is giving me an exception...can anyone help???..
I know you said you require an infinite loop, however you actually don't. Test out this code, it does exactly what you are looking for. The key to this is the global variable that is set "OUTSIDE" of your function. Note that I removed not only your code, but I also removed the buton variable from the function. If you remove my comments, the function is rather easy. var curval = 0;//initialize a counter variable to 0, which is the first position of an array. function ChangeAll(f,frm) { var mySplitResult = f.split(",");//convert list to array if(curval==mySplitResult.length-1)//if at the last position in the array, set the counter variable to the beginning of the array curval = 0; else//if not at the end, lets just add one and move to the next position in the array curval+=1; frm.value = mySplitResult[curval]; //set the value of the button to the value at the current selected (counter) position of the array } Code (markup):