Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
How do I redirect "this" in an event handler?
Old 12-13-2010, 04:20 PM How do I redirect "this" in an event handler?
Junior Talker

Posts: 1
Name: David
Trades: 0
Hi, I have a javascript object in which two DIV elements are created and kept private. I want to expose a function which allows the object users to bind a click event
handler to one DIV, but I can't make it work.
Here is my code:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>Test Page</title>
  <script type="text/javascript">
   function MyClass(){
    this._oDiv1 = document.createElement("div");
    this._oDiv1.style.cssText = "width:200px; height:150px; background-color:#0c0";
    document.body.appendChild(this._oDiv1);
    
    this._oDiv2 = document.createElement("div");
    this._oDiv2.style.cssText = "width:150px; height:30px; background-color:#f55; color:black; margin-top:10px; text-align: center";
    this._oDiv2.innerHTML = "Change to blue";
    document.body.appendChild(this._oDiv2);
   }
   MyClass.prototype.addClick = function(){
    this._oDiv2.onclick = function(){
     this._oDiv1.style.backgroundColor = "#00c"; //problem happens here
     return false;
    };
    //alert(this._oDiv2.onclick.toString());
   };
   
   function start(){
    var o = new MyClass();
    o.addClick();
   }
  </script>
 </head>
 <body onload="start();">
 </body>
</html>
The function I exposed is "addClick" in which the click event handler is bound to this._oDiv2. The handler just does a simple work to change this._oDiv1's background
color, but this can not be done because the "this" inside the handler points to the element receiving the "click" (in this case it's "_oDiv2"). I have no idea how to
make "this" inside the handler pointing to the current instance of "MyClass" object. Can anyone help me please?
netwind007 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-15-2010, 01:22 PM Re: How do I redirect "this" in an event handler?
Extreme Talker

Posts: 246
Trades: 0
Use the following.

MyClass.prototype.addClick = function(){
var that = this;

this._oDiv2.onclick = function(){
that._oDiv1.style.backgroundColor = "#00c";
return false;
}
};
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How do I redirect "this" in an event handler?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24578 seconds with 12 queries