Krishna Kumar
Krishna Kumar

Reputation: 2131

Function parameter not getting updated inside click event

Q: Function parameter not getting updated inside click event

**Event.js**

// main click event to call
$(document).on('click', '.start', function(){
    root.ajax({
        url: 'location'
    }, function( response ){
        root.update( response );
    })
});

**Content.js**

var flag = false;
root.update = function( response ){ 
    if(!flag){
        // event assignment for new created button
        $(document).on('click', '.innerStart', function(){
            // first time prints okay but after printing old value always
            // response is not getting updated
            console.log( response );
        });
        flag = true;
    }
}

Upvotes: 0

Views: 83

Answers (2)

poashoas
poashoas

Reputation: 1894

It looks like the flag variable is set to true, what makes the update run once.

Upvotes: 0

Evan Knowles
Evan Knowles

Reputation: 7511

Basically, the response variable is passed in the first time. You set the click event handler, which logs the response, and you never set the click handler again.

That response variable is never changed - the one that was set in the original click handler is always used, because that's the value you passed in. Instead, you could try set it as a variable, like:

**Event.js**
var response;
// main click event to call
$(document).on('click', '.start', function(){
    root.ajax({
        url: 'location'
    }, function( responseValue ){

        root.update( responseValue );
    })
});

**Content.js**

var flag = false;
root.update = function( responseValue ){    
    response = responseValue;
    if(!flag){
        // event assignment for new created button
        $(document).on('click', '.innerStart', function(){
            // first time prints okay but after printing old value always
            // response is not getting updated
            console.log( response );
        });
        flag = true;
    }
}

Upvotes: 1

Related Questions