Development More programming help requested, this time javascript

Discussion in 'Software' started by Zoon, 30 Mar 2009.

  1. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    Excuse the huge long post, but here's some code to test with.

    Basically, my function swapme() will show/hide the DIV, but if I include the ID tag in the <img ... /> tag it won't do anything at all.

    I am guessing that something is causing a javascript error and its just failing to run totally, but I've googled the code and found one that works with nearly identical line. The pictures are definitely in that path as you can see it referenced.

    Any help please? I'm very very new to javascript and have no idea how to even begin to debug this and chase down the fault with it.

    (NOW REMOVED AS ITS RESOLVED :D)
     
    Last edited: 1 Apr 2009
  2. BentAnat

    BentAnat Software Dev

    Joined:
    26 Jun 2008
    Posts:
    7,230
    Likes Received:
    219
    Hint: Use jQuery. just do it. It's easy to use and has methods for showing/hiding DIV's...
     
  3. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    But I don't need to use any javascript tools for something as simple as this surely?

    All I wanted to do was swap an image when you click the link.

    The image swapper doesn't work at all even when I remove the DIV swapping, but the DIV swapping works fine, and I can't see why! It doesn't even fire a javascript error, it just doesn't work!
     
  4. Shuriken

    Shuriken same christmas AV for a whole year

    Joined:
    1 Jan 2003
    Posts:
    1,312
    Likes Received:
    22
    Simple mistake my friend, in the javascript file the second "if" statement should be an "else if", at the moment both if statements are coming up as true (the first executes, and causes the second to be true).

    New script:
    Code:
    function swapme(title)
    {
        var swapimg = "img_" + title;
        var swapdiv = "div_" + title;
        
    	if (document.getElementById(swapdiv).style.display == "none")
    	{
    		document.getElementById(swapdiv).style.display = "block";
    		document.getElementById(swapimg).src = "skin/global/expanded.png";
    	}
    
    	else if (document.getElementById(swapdiv).style.display == "block")
    	{
    		document.getElementById(swapdiv).style.display = "none";
    		document.getElementById(swapimg).src = "skin/global/collapsed.png";
    	}
    }
    
    I've been coding JS for over 7 years, and I still make mistakes like that :wallbash:
     
  5. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,497
    Likes Received:
    630
    You sir, are a genius, thank you :D

    I knew it had to be something ridiculously obvious!!!

    Works wonderfully now :)

    I know its only a little thing, but when programming a cascading menu function you like to make sure the little touches are present :)

    Original code removed now so I guess this thread won't make much sense!
     

Share This Page