tags:

views:

62

answers:

2

I have two different links to fire a toggle function. However, they act independently:
Link 1: On
Link 2: On
Link 2: Off
Link 1: On
However, I would like them to work as if they were the same link:
Link 1: On
Link 2: Off
Link 2: On
Link 1: Off

Here is my current code... any help would be appreciated.

http://www.jsfiddle.net/Gavyz/

+2  A: 

Add a global toggled variable.

var toggled = false;

$(document).ready(function(){
    $("button").click(function(){
        if(toggled == false)
            change = 250;
        else
            change = -250;

        toggled = !toggled;

        $('p').animate({
            top: '+='+change
        }, 700) ;
    }
    );
});
Andrew M
A: 

Modified Andrew M's code so it works...

var toggled = false;

$(document).ready(function(){
    $("button").click(function(){
        if(toggled == false)
            change = 250;
        else
            change = -250;

        toggled = !toggled;

        $('p').animate({
            top: '+=' + change
        }, 700) ;
    });    
});

MetalAdam
whoops, I didn't select the last }); before I copied it. It worked fine without declaring change as a global var.
Andrew M
Updated, to ditch the extra line!
MetalAdam