user1109244
user1109244

Reputation: 107

Passing Variables Through JQuery Click Functions

How can I pass a variable from one click function to the other through JQuery?

 $(document).ready(function() {

       var n = 1;

        $('#Next').click(function() { 
        var nn = n++;

            });

        $('#Previous').click(function() { 

        alert(nn);

          });

            });

Upvotes: 0

Views: 413

Answers (2)

adeneo
adeneo

Reputation: 318352

As you already have a variable in a higher scope, use that, otherwise data() would be a better approach.

$(document).ready(function() {
    var n = 1;

    $('#Next').click(function() { 
        n++;
    });

    $('#Previous').click(function() { 
        alert(n);
    });
});

Upvotes: 1

koningdavid
koningdavid

Reputation: 8103

Replace

var nn = n++;

with

n++;

Upvotes: 1

Related Questions