From: euronay on
Hi

I'm pretty rubbish at java and I'm trying to get an image to change
when a user clicks a link.

I don't know why this doesn't work. Can anyone help?

foc.src=foc.src=='1.gif'?'2.gif':'1.gif';

foc is the image element - if i do foc.src='2.gif' it will quite
happily change but I want it to alternate between the two images every
time the link is clicked.

Thanks in advance

From: jshanman on
is this code in a function that is called on an "onclick" event? If so,
please post the HTML code, and the javascript function.

From: Ivo on
<euronay(a)hotmail.com> wrote
> foc.src=foc.src=='1.gif'?'2.gif':'1.gif';
>
> foc is the image element - if i do foc.src='2.gif' it will quite
> happily change but I want it to alternate between the two images every
> time the link is clicked.

Although you eset the src to '1.gif', the browser will make it the full
http://yoursite/path/1.gif.
So in the test, check that '1.gif' is part of the string, with
foc.src.indexOf( '1.gif' ) or so. If you have 11.gif and 21.gif, the check
needs to do a little more work.

--
hth
ivo
http://4umi.com/web/javascript/


From: euronay on
Cheers Ivo, that worked an absolute treat. :)

My first ever post on usenet and my problem solved in 15 minutes.
Awsome guys. Thanks.

From: Nays on
OK, so my little image script i managed to get working but not in
firefox. The Firefox java console says:

Error: foc.src has no properties
Source File: file:///.../test.htm
Line: 39

My code in full is:

function dsp(loc){
if(document.getElementById){
var foc=loc.firstChild;
foc=loc.firstChild;
if(foc.src.indexOf('down.gif')>0){
foc.src = 'up.gif';}
else{
foc.src = 'down.gif';}
foc=loc.parentNode.nextSibling.style?
loc.parentNode.nextSibling:
loc.parentNode.nextSibling.nextSibling;
foc.style.display=foc.style.display=='block'?'none':'block';}}

This is called like so:

<div class="header"><a href="javascript:void(0)" class="dsphead"
onclick="dsp(this)">
<img src="down.gif" border="0"></img>Test</a></div>
<div class="dspcont">
Content - yay!
</div>

Do any of you guys know how i can get the image 'src' value in firefox?
Or if this is impossible - detect the fact that it is firefox and go
straight to setting the divs style property because i know that the div
will work.

Thanks again