[thelist] Logic problem with a JavaScript image toggle...

Chris George cgeorge at basecorp.com
Fri Nov 7 12:36:23 CST 2003


Hi Simon,

Thanks for your insight.

I tried the other example you give, however, beyond the initial click
(which works), clicking again does not toggle it back.

I threw in an alert to see what path the img.src gives, and it's from
the root - therefore, the path doesn't seem to match the relative path
to the file specified, and I guess that means the first conditional is
being ignored...

Is that Internet Explorer 'filling in the blanks'?

Or?

Thanks again.

> -----Original Message-----
> From: Simon Willison [mailto:cs1spw at bath.ac.uk] 
> 
> I think your actual problem is being caused by a reliance on global 
> variables that cause interference between the first and second 
> times the 
> function is called. One alternative would be to do something 
> like this:
> 
> <a href="#" onclick="flip('image1'); return false;"><img 
> src="images/menuarrow_on.gif" id="image1" alt="+"></a>
> 
> function flip(imageID) {
>      var img = document.getElementById(imageID);
>      if (img.src == 'images/menuarrow_off.gif') {
>          img.src = 'images/menuarrow_on.gif'
>      } else {
>          img.src = 'images/menuarrow_off.gif'
>      }
> }


More information about the thelist mailing list