473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jun 27 '08 #1
1 1018
On May 17, 7:10 am, trib <andrew.be...@gmail.comwrote:
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.
[...]
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?
You want references from your handler to both the DOM object and the
"class". The this keyword can only reference one thing, you can set
it to whatever you like in the function call using call or apply, or
just let it set itself based on how you make the call.

Make up your mind what you what the this keyword to reference and do
that consistently. Then use a closure for the other reference.

You should be able to find general ways to do that in the archives, or
if you want to know how to do it using jQuery, use a jQuery group:

<URL: http://groups.google.com/group/jquery-en >
--
Rob
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Dufe | last post by:
Hello all: To deal with the problem of differing user screen resolutions, I've explored: 1) making the pages in PHP, 2) having different pages on the same page and selecting the proper one via...
11
by: Kalle Rutanen | last post by:
Hello Here is a short code snippet which does not compile. Could someone explain why this is ? class A { public: void set(int a) {
96
by: Karen Hill | last post by:
SELECT surgeries.*, animals.* FROM surgeries INNER JOIN animals ON .=. AND WHERE ((.=Date()) Or .=Date()); I'm trying to write a query that joins two table together, animals and surgeries...
14
by: John | last post by:
Hi We have an SBS2000 server which has an access database running internally, supporting around 20 users. The server is connected to a broadband connection. Is it viable for us to run an asp.net...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
28
by: H J van Rooyen | last post by:
Hi, I want to write a small system that is transaction based. I want to split the GUI front end data entry away from the file handling and record keeping. Now it seems almost trivially easy...
14
by: Bo Yang | last post by:
Following is my code: include <iostream> class Test{ public: Test(){}; void print(){ std::cout << "OK" << std::endl ; }; };
8
by: Blast | last post by:
I need help modeling schema for a particular issue that i've never run across before. And for the life of me, I cannot figure out how to model it - at least in a way that feels correct. Please let...
5
by: Mufasa | last post by:
How can you have different web.config files on your test machine vs. production? I realize I could not include it in the project but the problem is that if I make changes that go in both places I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.