bamboopanda bamboopanda - 1 year ago 63
jQuery Question

"this" changes value on click

If I have the following code:

function Something() {
this.randomElement = $("#element");

Something.prototype = {

functionOne: function() {

functionTwo: function() { {
this.functionOne(); //this becomes pointed at randomElement

How can I write this in a clean way where I wouldn't have to use
to replace
inside of functionTwo? Since the click event changes the value of this?

Answer Source

Because this is bound to the item that is clicked. You need to use bind this.functionOne.bind(this) ); 

or jQuery's proxy. $.proxy(this.functionOne, this) ); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download