0

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);

          });

            });

2 Answers 2

1

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);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Replace

var nn = n++;

with

n++;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.