...

View Full Version : Why does this if else statement not work?



tfburges
05-27-2009, 07:15 PM
function toggleImage(id,image1,image2) {
var img = (ie) ? document.all(id) : document.getElementById(id);
if (img.src == image1) {
img.src = image2;
} else {
img.src = image1;
}
}

Thanks,

Tim

tfburges
05-27-2009, 07:19 PM
Oh, and here's my the HTML:

<h2><a onclick="toggleID('HomeContent');toggleImage('homeToggle','/themes/default/hide.png','/themes/default/show.png')"><img id="homeToggle" src="/themes/default/hide.png"></a> <a href="index.php">Home</a></h2>

The else statement is always true.

tfburges
05-27-2009, 08:10 PM
Any ideas?

Philip M
05-27-2009, 08:52 PM
See Rule 5

5) Do not bump your thread repeatedly when you don't get a response. Sometimes when you post for help, you may not get a response in a timely matter, if at all. Forums aren't wishing wells, and some questions will fall through the cracks. That's a fact of life. It's ok to occasionally bump a thread, but only when done after an ample amount of time (ie: 2-3 days) have passed without a response, and never more than once, . Your thread is no more important than another member's when it comes to the amount of attention it should receive.

Old Pedant
05-27-2009, 09:34 PM
Did you debug?

Is it possible you are using that ID more than once on the page?

Did you put in one or more JS alert( ) calls to find out what is what?

At a minimum, I would have done this:


function toggleImage(id,image1,image2)
{
alert("using ie? " & ie);
var img = (ie) ? document.all(id) : document.getElementById(id);
alert( "id:" + id );
alert( "img.src: " + img.src );
alert( "image1: " + image1 + "\nimage2: " +image2);
if (img.src == image1) {
img.src = image2;
} else {
img.src = image1;
}
}

Most likely, img.src is null or undefined (presumably a problem with id) so of course you always fall through to the else.

How come you are using document.all?? You really care about people still using MSIE 4??? (Anything more recent than MSIE 4 supports document.getElementById.)

tfburges
05-27-2009, 09:52 PM
Sorry about that Phil. I bumped it not out of urgency but just in case someone thought it was solved because of the post I made before... glancing at the last line of that post might appear as if it was solved. It won't happen again. :thumbsup:

And thanks Old Pedant! That's a clever trick! The original URL includes the full (parent) path, while img.src does not. I'll do some research on it right now but I'll ask first here anyway just in case: how do I either include/add the full path to the src or remove it from the original URL? I don't really want to include the full path in the HTML itself if I can't help it.

And I don't care about IE4 users. I'll remove that.

Old Pedant
05-27-2009, 10:30 PM
Use indexOf:


function toggleImage(id,image1,image2)
{
var img = document.getElementById(id);
img.src = (img.src.indexOf(image1) >= 0 ) ? image2 : image1;
}

See, if img.src shows up as "http://www.yoursite.com/themes/default/hide.png" then using indexOf(img.src,"/themes/default/hide.png") will give you a non-negative "find".

randomuser773
05-28-2009, 12:06 PM
function toggleImage(id,image1,image2) {
var img = (ie) ? document.all(id) : document.getElementById(id);
if (img.src == image1) {
img.src = image2;
} else {
img.src = image1;
}
} The .src property contains the full path regardless of whatever relative path was assigned to it, so you can't expect an exact match:


if( img.src.match( image1 ) )
img.src = image2;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum