2

I have a little problem. I've created some element in an object and that works fine. But now I want to add some EventListeners to execute a function inner my object, the problem is that I cannot execute the function "setSelection". ("this" is not the "this" of the object, but the "this" of the event inner the click-function).

function Object() {
    this.init = function () {
        // delete table
        // ...
        // set table
        $('tr').append('<td id="1">test</td>');

        $('td').click(function() {
            // execute setSelection
        });
    }

    this.setSelection = function(x) {
        alert('You clicked id ' + x);
    }
}

$(document).ready(function() {
    x = new Object();
    x.init();
});
1
  • it can lead to problems to override the Object function, if you know what you are doing, fine, or else, you should rename it. For your problem, what causes this is that event callbacks are executed in the global context. This is why the proposed answers work, you need either to store the context in a var, or use functions as bind, apply or such to get the context back Commented Jan 19, 2017 at 14:56

4 Answers 4

1

You can fix this by storing the reference of this in the outer scope to a variable that you can use in the click event handler:

function Object() {
    var _this = this;

    this.init = function () {
        // delete table
        // ...
        // set table
        $('tr').append('<td id="1">test</td>');

        $('td').click(function() {
            _this.doSomething();
        });
    }

    this.setSelection = function(x) {
        alert('You clicked id ' + x);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
    $('td').click((function(e) {
        // execute setSelection
        this.setSelection(...);
    }).bind(this));

1 Comment

This would work, but it would mean that you would need to use e.target to get a reference to the td which was clicked, if needed.
0

My approach will be:

function Object() {
  this.init = function () {
    // delete table
    // ...
    // set table
    $('tr').append('<td id="1">test</td>');

    $('td').click(function() {
      setSelection(this);
    });
  }

  setSelection = function(x) {
    alert('You clicked id ' + x);
  }
}

Comments

0

I've solved the problem...

function Object() {
    this.init = function () {
        // delete table
        // ...
        // set table
        $('tr').append('<td id="1">test</td>');

        this.setSelection();
    }

    this.setSelection = function() {
       $('td').click(function() {
            alert('You clicked id ' + $(this).attr('id'));
        });
    }
}

$(document).ready(function() {
    x = new Object();
    x.init();
});

1 Comment

This is not a solution to your question. And this is wrong - every call of setSelection adds a new click handler to td elements. All these handlers will be executed in sequence the next time your click.

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.