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

Simon Willison cs1spw at bath.ac.uk
Fri Nov 7 12:54:20 CST 2003


Chris George wrote:
> 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...

Aah I'd forgotten about that. You can fix that using a string find 
operation:

function flip(imageID) {
     var img = document.getElementById(imageID);
     if (img.src.indexOf('menuarrow_off.gif') > -1) {
         img.src = 'images/menuarrow_on.gif'
     } else {
         img.src = 'images/menuarrow_off.gif'
     }
}



More information about the thelist mailing list