View Full Version : What is the image number?

12-27-2003, 11:03 PM
I have some images on the screen, displayed from the array im[ ].
When I click on an image, I would like that the onclick= function stores the image number in a cookie named 'showpic'. What is the way to get the number of the image I clicked on?

<script language="JavaScript">
for (var i = 1; i <= maxim; i++)
document.write('<img src="'+im[i]+'" vspace="5" hspace="5" width="100" height="75" border="1" alt="" onclick="set(\'showpic\',i);">');

Thanks in advance

12-27-2003, 11:21 PM
When the user clicks the image, you could get it like this:

function getImageNum(n) {
var src = n.getAttribute('src');

for(var i = 1; i <= maxim; i++) {

if(im[i] == src) {

return i;




Then, onclick you could set the cookie:

<img ... onclick="setCookie('imgnum', getImageNum(this));" />

Hope that helps!

Happy coding! :)

12-28-2003, 12:24 AM
Thank You so much nolachrymose, it works! :thumbsup:
In fact, the variable src in your answer contained already what I needed.
May God bless you.