Google Groups Home
Help | Sign in
Message from discussion How can 'this' be two different things
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
trib  
View profile
 More options May 16, 5:10 pm
Newsgroups: comp.lang.javascript
From: trib <andrew.be...@gmail.com>
Date: Fri, 16 May 2008 14:10:39 -0700 (PDT)
Local: Fri, May 16 2008 5:10 pm
Subject: How can 'this' be two different things
Can anyone help with this problem?

I have a class that adds an event handler on a DOM object, and that
event handler calls a method of the class.  I'd like the handler
function to run in the scope of the same object instance that assigned
the handler to the DOM element (since there may be more than one
instance on the page).  But I also still need to be able to access a
reference to the object on which the event occured.  I know how to do
either one of these, but not both!

The practical application of this is a dynamic table widget that
combines an HTML table with a form, allowing the user to add new rows
to the table.  Each row also gets a delete button appended and that's
what I'm assigning the click event to.  When the button is clicked the
JS should delete the relevant row from the table and remove some data
from an array.

This is what I've got (using Jquery):

$("#"+this.tblid+"
img.deletebtn").click(this.deleteHandler.bind(this));

vs:

$("#"+this.tblid+" img.deletebtn").click(this.deleteHandler);

The former makes 'this' a reference to the instance, while the latter
makes it a reference to the image being clicked.  I think.  Anyhow, I
need references to both.  Am I approaching this the wrong way or is
there a solution?

Cheers,

Andrew


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google