Click to See Complete Forum and Search --> : hardcoding a for loop variable


flashgroover
03-05-2003, 04:59 PM
i have a FOR loop that duplicates the same image across the screen. on each of these images i want an onClick event that sends a variable to a function that pertaines to the spacific image i've clicked.

my problem is that im trying to use the variable used in the FOR loop as my variablwe i want to pass. im not sure if this is correct and i hope i have explained it okay. the JS is below with a few other comments within it. thanks for the help.

<tr>
<SCRIPT LANGUAGE="JavaScript" type="text/JavaScript">



for (m=1; m<= 10; m++) {

document.write("<td>");

document.write("<a href='#' onClick='alert(m);'>");
// using alert() for quick checking. need (m) to always be 1 on the first pic and 2 on the second and so one. these nubers are going to be added with a ".jpg".

document.write("<img src = 'preview.gif' border='0''>");
document.write("</td>");

}

</SCRIPT>
</tr>

if you feel like chatting me through it, try Windows Messenger - tickers99@hotmail.com

dabush
03-05-2003, 05:06 PM
this ought to do the trick!


<BODY>
<table>
<tr>
<script language=JavaScript>
<!--
for (m=1; m<=10; m++)
{
document.write('<td><a href="javascript:alert('+m+');"><img src=preview.gif border=0></a></td>');
}
// -->
</script>
</tr>
</table>
</BODY>

dabush
03-05-2003, 05:08 PM
for some reason, in that really long line, it automatically put a space between java and script. remove that space.

flashgroover
03-05-2003, 05:25 PM
dabush u the man! thansk ALOT

cesar