0

I need to access local variable returned by function inside a chained function

ex.

$("#history_table").bind("sortStart", function() {
     var a=30;
     return a;
}).bind("sortEnd", function() {
     alert(a);               
});

here in this example I need to access variable a returned by the first function, sortStart and aortEnd events will trigger the two functions asynchronously...

1 Answer 1

3

The variable needs to be declared outside:

var a = 0;
$("#history_table").bind("sortStart", function() {
     a=30;
     return a;
}).bind("sortEnd", function() {
     alert(a);               
});

or make it as property of the current object using the .data() function:

$("#history_table").bind("sortStart", function() {
     var a = 30;
     $(this).data('a', a);
     return a;
}).bind("sortEnd", function() {
     var a = $(this).data('a');
     alert(a);               
});
Sign up to request clarification or add additional context in comments.

1 Comment

I think the .data() approach is the one that makes sense. Returning a value from an event handler - even a "custom" event handler - really doesn't make much sense, unless the idea is explicitly to control bubbling in some dynamic way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.