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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with ImageSwap/Switch

    Hi I have this code to collapse and expand a <div>.


    <a href="javascript:collapse1.slidedown()" onClick="switchImage ('images/expand.png','images/collapse.png','pass_image'); javascript:collapse1.slideup();"><img src="images/expand.png" id = "pass_image" name="pass_image" border="0"/>Slide Down</a>
    <div id="test" style="width: 300px; background-color: #2e2e2e;">




    This is the javascript file.

    function switchImage (image_1,image_2,image_id)
    {
    if (document[image_id].src == image_2)
    {
    document[image_id].src = image_1;
    }
    else
    {
    document[image_id].src = image_2;
    }
    }

    What is does it is when it is clicked to expand it shows the collapse pic and when it is click again to collapse it shows the expand. But the problem is when i click for it to expand it shows the collapse icon like it should but when I click it again it does not move back to the original expand icon?

    Any help would be great. I think it is something simple i am missing.

    Thanks,
    Tyler

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    the src attribute returns the complete url of the image src so when comparing 'http://www.example.com/images/collapse.png' == 'images/collapse.png' always fails

    instead try using
    Code:
    function switchImage (image_1,image_2,image_id) { 
    	if (document[image_id].src.indexOf(image_2) != -1 ) {
    		document[image_id].src = image_1;
    	} else {
    		document[image_id].src = image_2;
    	}
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Worked!!

    Thanks, That worked perfectly!!!


  •  

    Posting Permissions

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