Link to home
Start Free TrialLog in
Avatar of Kalle 2
Kalle 2

asked on

Image Array - OnClick problem

I have a table with images ranging from 1 to 10 images.When clicking the images they should change into anotherimage (Light and Dark). This is the code for one of the images:<A HREF="" TARGET=""
onClick="PictureClick(??)">
<img src="Image1.gif" align="bottom"
width="113" height="45" border=0></a>
And this is the code for PictureClick:function PictureClick(n){
  if (document.images[n].src==OnImages[n].src)
  {
     document.images[n].src=OffImages[n].src
  }
  else
  {
     document.images[n].src=OnImages[n].src
  }I need the PictureClick to send the current imagenumber as an argument.If that is impossible I need to find out which image was clicked in my function.Can anybody give me some help on this one?                     // Anders Karlsson, Sweden
Avatar of dj|nn
dj|nn

ok, if i understand your question.  there are 10 images.  each image have a active and deactive.  you want the image to change when clicked?

if you want don't want to automatically set the number for picture click you will have to have the code for the images generated dynamically.

<head>

<script language=JavaScript>
<!---

function PictureClick(n) {
  s = document.images[n].src;
  if (s.indexOf('_l') != -1) {
    document.images[n].src = 'image' + eval(n+1) + '_d.gif';
  }
  else {
    document.images[n].src = 'image' + eval(n+1) + '_l.gif';
  }
}

var t;

for (i=0; i < 10; i++) {
  t = '<tr><td>'; // i'm guessing at your table formatting
                  // you will probably want to change that
  t += '<a href="javascript:PictureClick(' + i + ')" ';
  t += 'onClick="return false;">';
  t += '<img src=image' + eval(i+1) + '_l.gif align=bottom ';
  t += 'width=113 height=45 border=0></a>';
  t += '</td></tr>\n';
}

// --->
</script>

</head>

then in the body of your page:

<table>

<script language=JavaScript>
<!---
document.write(t);
// --->
</script>

</table>
Avatar of Kalle 2

ASKER

Thanks for your answer. Problem is I cant generate the HTML dynamically. I am dependant on another application(not written by me) that fills my HTML with differentimages (depending on the user logged on). The number ofimages can vary as well, from one to ten. And I do not knowwhich images it will be until I see the final HTML code.I found a new way of doing this though. But I have anothersmall problem and I will award you your points if you solvethis one (might be very easy).I need to know How many images there are in my document.I use this code:  ix=0;
  while (sImage != document.images[ix].src  && ix<99)
    ix=ix+1;
If I dont find the Image I need the while loop to quitwhen it has checked all of the images on the document.In this case I hardcoded it to 99 (above). Is there a propertythat tells me the number of images on the document?                                  // Anders Karlsson
ASKER CERTIFIED SOLUTION
Avatar of pmcintos
pmcintos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kalle 2

ASKER

Thank you!
             // Anders Karlsson