Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swaping image js help 2

Status
Not open for further replies.

snowboardr

Programmer
Feb 22, 2002
1,401
PH
I attempted to write a script to swap a plus / minus button based on what its src is... it was swaping it to minus, but its not swapping it back... but now its not doing anything ???
Any ideas... or can this be done this way?

ps. I am terrible at js.

Code:
<script type="text/javascript">
function SwapImage(imgid) {
if (document.getElementById(imgid).src == "images/forum/btns/plus_syn2.gif")
document.getElementById(imgid).src = "images/forum/btns/minus_syn2.gif";
}
Else
{
document.getElementById(imgid).src = "images/forum/btns/plus_syn2.gif";
}
</script>

Regards
 
You cannot check your src being exact to something unless you put in the full path to the image, for example:

Code:
if (img.src == 'images/someting.gif')

will never work, because as far as the browser is concerned, the real source is:

Code:
'[URL unfurl="true"]http://www.mysite.com/images/someting.gif'[/URL]

So you'll need to use "indexOf" to test for the src:

Code:
if (img.src.indexOf('images/someting.gif') != -1)

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
It doesn't seem to be working...

Code:
<script type="text/javascript">
function SwapImage(imgid) {
If (document.getElementById(imgid).src.indexOf('images/forum/btns/plus_syn2.gif') == -1)
document.getElementById(imgid).src = "images/forum/btns/minus_syn2.gif";
}
else
{
document.getElementById(imgid).src = "images/forum/btns/plus_syn2.gif";
}
</script>
 
posted wrong code this is what i have..

Code:
<script type="text/javascript">
function SwapImage(imgid) {
If (document.getElementById(imgid).src.indexOf('images/forum/btns/plus_syn2.gif') != -1)
document.getElementById(imgid).src = "[URL unfurl="true"]http://localhost/0/images/forum/btns/minus_syn2.gif";[/URL]
}
else
{
document.getElementById(imgid).src = "[URL unfurl="true"]http://localhost/0/images/forum/btns/plus_syn2.gif";[/URL]
}
</script>
 
Also, you're missing an opening brace after the first if, and a closing brace for the function. Try this for size:

Code:
<script type="text/javascript">
function SwapImage(imgid) {
<!--
	var myImg = document.getElementById(imgid);
	if (myImg.src.indexOf('images/forum/btns/plus_syn2.gif') != -1) {
		myImg.src = '[URL unfurl="true"]http://localhost/0/images/forum/btns/minus_syn2.gif';[/URL]
	} else {
		myImg.src = '[URL unfurl="true"]http://localhost/0/images/forum/btns/plus_syn2.gif';[/URL]
	}
}
//-->
</script>

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
As Dan was pointing out, you need to use a lower case "i" for the javascript operator "if":
Code:
<script type="text/javascript">
function SwapImage(imgid) {
[s]If[/s] [!]if[/!] (document.getElementById(imgid).src.indexOf('images/forum/btns/plus_syn2.gif') != -1)
...
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top