0

I know this question has been asked many times but I haven't been able to resolve from what I found on Stack O. Here is my code

for(var i=0; i<retrievedSearchesListLength; i++){
retrievedSearchesListProv = retrievedSearchesList[i].searchId;
retrievedSearchesListType = retrievedSearchesList[i].searchParameters;

    function getEventHandlerFunction(a){
    $J.cookies.set('ps_clickedsearch',a);
}

 $J('#submitSearch'+i).bind('click',getEventHandlerFunction(retrievedSearchesListType));
}

Everytime the resulting value is the last for loop value. How do I keep scope so that the link clicked results in the correct value?

I need the correct retrievedSearchesListType to reflect when the link is clicked.

Thanks in advance

1 Answer 1

1

You need to return the callback method from getEventHandlerFunction

for(var i=0; i<retrievedSearchesListLength; i++){
    retrievedSearchesListProv = retrievedSearchesList[i].searchId;
    retrievedSearchesListType = retrievedSearchesList[i].searchParameters;

    function getEventHandlerFunction(a){
        return function(){
            $J.cookies.set('ps_clickedsearch',a);
        }
    }

    $J('#submitSearch'+i).bind('click',getEventHandlerFunction(retrievedSearchesListType));
}
Sign up to request clarification or add additional context in comments.

Comments

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.