Kazzin
Kazzin

Reputation: 17

Comparing old and new value of the same variable in jquery

I coded a website using Ajax and I want to compare clicked links to avoid page reloading. My way to do it is having a var called Link and storing url into it. Everytime I click on the #button, the url is stored into the Link, then I call the CompareLink() function, then the GoToLink() function.

var Link;

$('#button').click(function() {
    Link = http://myurl ;
    CompareLink();
    GoToLink();
}

I have trouble with the data comparison. I want to compare the OLD value of Link with the new Value, so I wrote a very vague approach (nonworking) and I wanted to know if somebody could help me.

function CompareLink() {
    if ( Link == .data(Link)) {
        //execute code
    }
}

Upvotes: 1

Views: 2595

Answers (2)

Cati Lucian
Cati Lucian

Reputation: 1

var data = {}
var count = 0;
data['name' + count++] = "value1" -> name1
data['name' + count++] = "value2" -> name2

get old value

data['name' + (count - 1)] ->  'value1'

Upvotes: 0

Rorchackh
Rorchackh

Reputation: 2181

how about this?

<a href='something here' class='button' >something</a>
<a href='something else here' class='button' >something else</a>​


var link = null, oldLink = null;
$('.button').click(function() {
    link = $(this).attr('href');
    if (oldLink == null) {
         oldLink = link;
    }
    CompareLink(link, oldLink);
    return false;
}

and then

function CompareLink(link, oldLink) {
    if ( link == oldLink) {
        // do something now
    }

    oldLink = link;
}

function GoToLink(link) {
    window.location.href = link;
}

checkout this fiddle I made: http://jsfiddle.net/V9DyW/

Upvotes: 2

Related Questions