Zholen
Zholen

Reputation: 1790

Javascript namespace issue, how do i get this value within this function call

im just not sure how to get the desired value within this function Thanks!

temp = exotics[i].split(',');

if ($.inArray(temp[0], tblProbables) != -1) {
    item = $("<li><a id='" + temp[0] + "'" + "href='#' >" + temp[1] + "</a></li>");
    item.click(function () { GetProbable(temp[0]); });
}

temp[0] is blank because it doesn't exist within that function space.... so how can i setup the click event so that it calls the function with the proper param?

Upvotes: 0

Views: 78

Answers (2)

RightSaidFred
RightSaidFred

Reputation: 11327

I have a feeling you're reusing the temp variable somehow.

If so, you can just create a new variable scope in a function that returns a function that references the proper scoped variable.

function createHandler( temp ) {
    return function () { GetProbable(temp); };
}

item.click( createHandler(temp[0]) );

Upvotes: 3

Rob W
Rob W

Reputation: 349142

Store the value in a temporary variable:

....
    var temp0 = temp[0];
    item.click(function () { GetProbable(temp0); });

If the the temp0 varible changes multiple times (e.g. in a loop), create a closure:

    (function(temp0){ //Inside this function, it doesn't matter if `temp` changes
        item.click(function () { GetProbable(temp0); });
    })(temp[0]);

Upvotes: 1

Related Questions