Dom I Nic
Dom I Nic

Reputation: 71

Store variable from one event handler to be used by another event handler

I have this code:

$("#open").click(function()
{
 var pos = $(window).scrollTop();
 /* the next lines of code affects the value
  * of 'pos'.
  */
});

$("#close").click(function()
{
 var pos = /* what i want here is the $(window).scrollTop(); 
            before the #open event handler changes it's value. */
 //another code of mine.
});

Can anyone help me with this? Thanks in advance.

Upvotes: 2

Views: 1863

Answers (5)

Chris Moschini
Chris Moschini

Reputation: 37947

I'd use a variable local to an anonymous function:

(function() {
    var context = {};
    $('#open').click(function() {
        context.pos = $(window).scrollTop();
    });
    $('#close').click(function() {
        // Do something with context.pos
    });
})();

Your code and explanation don't make this clear, but the above maintains an assumption from your code: That close cannot be clicked unless open has been clicked, and open cannot be clicked again until close has been clicked. But that really depends on your code - if that assumption isn't true I'd approach it differently (if that assumption isn't true, clicking close here risks getting an undefined).

Upvotes: 1

Daniel Conde Marin
Daniel Conde Marin

Reputation: 7742

You could just organize a little your code and do this:

function MyApp(){
   var self = this;

   this.pos = "";
   this.temp = $(window).scrollTop();   //store initial value 

   this.wire = function(){
      $("#open").click(self.open);
      $("#close").click(self.close);
   }

   this.open = function(){
     self.pos = $(window).scrollTop();
      /* the next lines of code affects the value
       * of 'pos'.
       */
   }

   this.close = function(){
     self.pos = /* what i want here is the $(window).scrollTop(); before
            * the #open event handler changes it's value.
            */
        //another code of mine.
     var original = self.temp;  //here get the initial stored value
   }


}

Example:

<script type="text/javascript">
    $(function() {
        var app = new MyApp();
        app.wire();         //wire handlers
    });
</script>

Upvotes: 1

Priyank
Priyank

Reputation: 1384

More safe way.

 $("#open").click(function() {
            $("#close").attr("data-pop", $(window).scrollTop());

        });

        $("#close").click(function() {
            var pos = $(this).attr("data-pop");
        });

Upvotes: 0

scrowler
scrowler

Reputation: 24406

You can store the original window DOM contents before you call any functions:

var pos;

$("#open").click(function(){
    pos = $(window).scrollTop();
});

$("#close").click(function(){
    $(window).height(pos);
});

Docs

Upvotes: 1

A.T.
A.T.

Reputation: 26312

it's very simple, make variable global

var pos; //global variable
$("#open").click(function()
{
 pos = $(window).scrollTop();
 /* the next lines of code affects the value
  * of 'pos'.
  */
});

$("#close").click(function()
{
 // use pos here accordingly
 //another code of mine.
});

Upvotes: 2

Related Questions