0

I have a Javascript object the following:

function extraFields(id) {
    this.numActiveFields = 0;

    ...

    this.addRow = function(quoteClass, rowClass) {
        var remButton = $("<span></span>")
        .addClass(rowClass)
        .addClass(quoteClass)
        .click(function() {
            //here I want to refer to the object's variable, but instead refer
            //to the JQuery object
            this.numActiveFields++;
        });
    }

    ...
}

I want to change the object's variable from inside the callback function. How would I do that? Should I change the way I declare the object?

1

1 Answer 1

1

When you are in the callback function "this" is the "remButton" you created. Just save "this" in a variable before the callback and then use that instead.

For a better explanation please look at the link that mplungjan suggested: Reference to an object from a callback function in jQuery

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was sure there was a thread like that, but couldn't find it.

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.