0

Using the following code I'm unable to get value of this.select inside the calling function change... see below

function getSelection(selectionType) {
    this.select = selectionType;
    alert(this.select); // works
    this.getFile = function() {
        $(".file").change(function() {
            alert($(this).attr("id")); // works
            alert(this.select); // says undefined
            if ($(this).attr("id") == this.select) {
                alert("test"); // no display
            }
        });
    };
}​

1 Answer 1

4

cache this:

function getSelection(selectionType) {
    var that = this; // <========================
    this.select = selectionType;
    alert(this.select);

    this.getFile = function() {
        $(".file").change(function() {
            alert($(this).attr("id"));
            alert(that.select);
            if ($(this).attr("id") == that.select) {
                alert("test");
            }
        });
    }
}​
Sign up to request clarification or add additional context in comments.

1 Comment

wow, your solution worked but don't understand why. thanks gdoron.. can't accept the answer, I have to wait 5 more minutes

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.