Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts

    Why does this if else statement not work?

    Code:
    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

  • #2
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts
    Oh, and here's my the HTML:
    Code:
    <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.

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts
    Any ideas?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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:
    Code:
    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.)

  • #6
    Regular Coder
    Join Date
    May 2009
    Posts
    425
    Thanks
    3
    Thanked 62 Times in 61 Posts

    Lightbulb

    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.

    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Use indexOf:
    Code:
    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".

  • #8
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by tfburges View Post
    Code:
    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:

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •