How in jQuery to pass parameters to a function after fadeOut function

Hi, how can I pass a variable to this function

var _index = $("#sideNewsContent_menu li").index($(this)) + 1;

$("#sideNewsContent_body").fadeOut(300, function (index) {

     //HERE i want to use variable _index
});
+3
source share
2 answers

you can use the fact that "_index" can be used in the closure you define:

var _index = $("#sideNewsContent_menu li").index($(this)) + 1;
$("#sideNewsContent_body").fadeOut(300, function () {
 alert(_index);
});

In javascript, when you define an anonymous function (closure), you can use all the variables that exist in the current context (except for "this", which is a special keyword).

+4
source

This is already in the area. Just do not var _indexplace functions anywhere or create a local variable with a name _indexthat will take precedence over the variable _indexin the closure.

var _index = $("#sideNewsContent_menu li").index($(this)) + 1;

$("#sideNewsContent_body").fadeOut(300, function (index) {

     _index++;

     alert(_index);
});

Do not do this:

$("#sideNewsContent_body").fadeOut(300, function (index) {

     var _index = _index++;

     alert(_index);
});
+1
source

All Articles