0

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;
    }
}
2
  • What is the workflow ? You click '.start', you click '.innerStart', the console print "something". You click '.start', you click '.innerStart', the console print ??? (and you expect ???) ? Commented Oct 17, 2014 at 8:05
  • click on .start I will get an popup window and in new window there are many .innerStart, when I click on .innerStart then updated value of response should be displayed Commented Oct 17, 2014 at 10:36

2 Answers 2

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.