473,405 Members | 2,176 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,405 software developers and data experts.

event handling error

dmjpro
2,476 2GB
my HTML code ...
Expand|Select|Wrap|Line Numbers
  1. <table border="0" cellpadding="0" cellspacing="0">
  2. <tr>
  3.     <td id=tabbed_TD>Debasis Jana</td>
  4. </tr>
  5. </table>
  6.  
and my JS code ..
Expand|Select|Wrap|Line Numbers
  1. function TDClass(){
  2.     this.handle = function(){
  3.         alert(this);
  4.     }
  5. }
  6.  
  7. function test(){
  8.     document.getElementById('tabbed_TD').addEventListener('click',new TDClass().handle,false);
  9. }
  10.  
Now my question is ... inside handle function this says the current clicked TD cell..how? As it supposed to say TDClass object itself.
Please explain..
Jul 30 '10 #1
5 1132
Dormilich
8,658 Expert Mod 8TB
As it supposed to say TDClass object itself.
it’s not, all event listeners (and some other functions like setTimeout, setInterval) change the scope of the current object (this is no bug, but intended).

this behaviour allows you to execute code on a DOM Node without explicitly writing the code to that object beforehand. i.e. it allows you to create re-usable code.

JavaScript Scope @ Digital Web Magazine

if you need to preserve the object’s context, you have to use Closures.
Expand|Select|Wrap|Line Numbers
  1. vat tdc = new TDClass();
  2. document.getElementById('tabbed_TD').addEventListener('click', tdc.handle.bind(tdc), false);
Expand|Select|Wrap|Line Numbers
  1. Function.prototype.bind = function (obj) {
  2.     var fn = this;
  3.     return function () {
  4.         return fn.apply(obj, arguments);
  5.     }
  6. };
note: standard compliant browsers still pass the event object as first argument to the function.
Jul 30 '10 #2
dmjpro
2,476 2GB
This link is real GEM. Some absurd/typical things just used to go beyond my brain. Those things, exactly those things...context of execution, this in different context and more over apply and call, the clear concept of those hazy things are now clear.

It would be grateful, if you do little more help...

Expand|Select|Wrap|Line Numbers
  1. <input type=button value=' Button ' onclick='e_handler(12,34,"debasis jana")' />
  2.  
In the above code e_handler takes 3 inputs, if I register the e_handler through Javascript function then how would I pass those 3 parameters?
Is it possible?
Aug 3 '10 #3
Dormilich
8,658 Expert Mod 8TB
you have to use a Closure or a Partial.
Aug 3 '10 #4
dmjpro
2,476 2GB
Ahh! ..so there is no direct way. What you mean by partial??
Aug 3 '10 #5
Dormilich
8,658 Expert Mod 8TB
a Partial is related to the Lambda-Calculus (something awfully complicated).

a Partial implementation
Aug 3 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tim Gallivan | last post by:
Hi group, I'm having a strange problem with event handling. My project has a form with a textbox that creates 25 instances of class A when a button is clicked. Class A raises the event without...
4
by: hillcountry74 | last post by:
Hi, I'm a newbie and trying to understand event handling in c#. I have understood handling events using delelgate objects. But not this method- "Event handling by overriding the virtual...
3
by: Ashok Kumar K | last post by:
Hi all, Where can I get some insight on using the __hook, __unhook, event_source and event_receiver for specifically COM events. The documentation given in MSDN is very minimal. I have the...
3
by: Nikhil Patel | last post by:
Hi all, I have a drop down in an asp.net form defined as below: <asp:DropDownList id="dropDownLeaseType" runat="server" OnSelectedIndexChanged="LeaseTypeChanged"></asp:DropDownList> I have...
9
by: Sridhar | last post by:
Hi, I have created a web page which includes a place holder. I also have a dropdown list in that webpage. when I select one of the choices in that dropdown list, It will load a user control...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
4
by: reggiestyles | last post by:
Hi, I've got a question about prototype and event handling. I've got several div's (dynamic number) on a page that I want to set as active or inactive (basically, I'm using scriptaculous'...
4
by: StepanM | last post by:
Problem with event handling in FireFox. I have two controls on HTML page: ListBox and TextBox (<selectand <input>). Both controls have event "onchange" and functions for this event (e.g....
0
karlectomy
by: karlectomy | last post by:
Hi, I just recently started working with Flash CS3 and decided to try my fate in AS3. I did my research and read my sources and still I am having a problem with event handling on a button. I want...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.