Hi everyone, Please observe the below code fun(){ for(i = 1; i < = j; i++){ select = document.forms['MessageNew'].userId.options.value; selection = '<a href="javascript:assignValue(select);"> + select + </a>'; } } I am calling function fun(), for loop executes and number of links will appear depending on the value of j.What i am trying to do is, i need to pass 'select' value for each links when i click the link but only final select value i.e for i=j, is going.so if i click any link only last select value(i=j) will go because looping has completed. i need 'select' values for all the valuse of i. Do i need to give id to anchor ? how to approach this?
Hi Bobby..thanks a lot for your response... Please observe the below code function fun(){ for(i = 1; i < = j; i++){ select = document.forms['MessageNew'].userId.options.value; selection = '<a href="javascript:assignValue(select);"> + select + </a>'; } } Assume j = 10, looping will happen for 10 times and 10 links will appear. Let us assume value of 'select' is 'ABCD' when i = 1 and at i = 10, value of select will be 'XYZ' . Observe that in href we are calling one javascript function assignValue(select) and passing select in that. This javascript will be called when i click the link, even i click link number 1, XYZ will be passed to the assignValue() since looping is completed. But i need ABCD to be passed when i click link 1 and the respective values should be passed for link2,link3...,link10. I hope you got my prob..Please help me how can i approach the solution.
Hi I am still not really sure what you want, but maybe: function fun(){ var j = 10; var select; for(i=1;i<=j; i++){ select = document.forms['MessageNew'].userId.options.value; selection = '<a href=" + select + " onclick="assignValue(select)"> + select + </a>'; } }
You should fix at least this: - Use '<=' instead of '< =' (no spaces inside) - You select variable must be outside your string. - The solution is different depending of the type of parameter (numeric or string) of assignValue function. Take this html as example: <html> <head /> <body onload="fun()"> <script type="text/javascript"> var value = [ "", "AB", "CDE", "E" ]; var selectionNumber = [ 0, 0, 0, 0 ]; var selectionString = [ "", "", "", "" ]; function fun() { for(i = 1; i <= value.length; i++) { select = value[i]; selectionNumber[i] = '<a href="javascript:assignValue(' + select + ');">' + select + '</a>'; selectionString[i] = '<a href="javascript:assignValue(\'' + select + '\');">' + select + '</a>'; alert( "sn='" + selectionNumber[i] + "'\nss='" + selectionString[i] + "'" ); } } </script> </body> </html> Code (markup):