473,783 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing this to a event function


i've got this for IE:

node.onchange = foo;

but really i'd like to pass node object to foo function also.

in html i would do onchange="foo(t his)"...but this has to
be done in javascript at runtime.

Aug 29 '06 #1
3 1345
ppcguy wrote:
i've got this for IE:

node.onchange = foo;

but really i'd like to pass node object to foo function also.

in html i would do onchange="foo(t his)"...but this has to
be done in javascript at runtime.
You don't need to as - this - inside - foo - will already refer to your
- node - object when it is called as the onclick handler for - node -.

This works because when in HTML you do:-

<X onclick="foo(th is);">

- the browser used the string value of the onclick attribute as the
body for a function it creates internally and assigns to the - onclick
- property of the element. So the above is equivalent to:-

node.onclick = function(){
foo(this);
};

(where - node - refers to the element that would have the onclick
attribute otherwise). And then when the browser responds to a click
event it calls this function assigned to the - onclick - property of
the element as a method of the element so - this - refers to the
element.

Richard.

Aug 29 '06 #2
ppcguy wrote:
i've got this for IE:

node.onchange = foo;

but really i'd like to pass node object to foo function also.

in html i would do onchange="foo(t his)"...but this has to
be done in javascript at runtime.
function foo(){
var theNode = this;
}

When you do:

node.onchange = foo;

the value of node's onchange attribute is assigned a reference to foo, a
function object. Effectively you you have:

node.foo

When you fire node's onchange event, foo is called like this:

node.onchange() ;

which is (more or less) the same as:

node.foo();

so that foo's 'this' operator will reference 'node'.

If you just call foo(), you are effectively calling window.foo() and its
this operator will reference the window/global object.
--
Rob
Aug 29 '06 #3
ppcguy wrote:
i've got this for IE:

node.onchange = foo;

but really i'd like to pass node object to foo function also.
Event listeners assigned in that manner are called as properties of the
node, therefore the value of the this operator already /is/ a reference
to the element.

Using the addEventListene r method (W3C DOM) will also cause the this
operator to be set to the element, but with the attachEvent method (MSIE
DOM), the operator will refer to the global object.

[snip]

Mike
Aug 29 '06 #4

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

Similar topics

7
49582
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
2
2912
by: Kyzer | last post by:
Hi all, I have created a class which contains many TShapes, TLabels and TPanels. What I want to be able to do is when creating this class, is passing in a pointer to OnMouseDown and OnMouseUp events. Thereby, the user of the class can create their own event handlers for those events. The idea is that by passing in the event handlers, these event handlers are assigned to the appropriate events for each of the objects in the class. ...
0
3291
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer = 0ms.....60ms. I'm already timing the callbacks using the Win32API QueryPerformanceCounter/Frequency. The code I have is actually performing the callback and it looks like it is close to 1ms but I now need to get my object passed to this static...
6
3954
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I have some public functions and private variables. Inside the class I also have a declaration of a new form object. One of the functions of the class takes that form object, shows it with showdialog and the basically passes the control to the form...
7
1984
by: Bonzo | last post by:
>From within a function, I want to pass a/some parameters to another function, AND all arguments, passed into this function. e.g. function firstFunction(){ //this function may have been passed, 0, 1, or n arguments... ... //call another function... var someCalculatedValue = 'someValue';
2
1574
by: diffuser78 | last post by:
I wrote a small app in wxPython using wxGlade as designer tool. wxGlade brings and writes a lot of code by itself and thats where my confusion started. Its about parameter passing. Here is my little confusion. ***************CODE BEGINS************************************ class MyFrame1(wx.Frame): def __init__(self, *args, **kwds): ..... ..... def OnEdit(self, event):
4
2385
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript. Because I want to use some ajax-requests and other javascript-functions on my xhtml, I need to dynamically add event handlers to any possible dom-elements. All solutions I found so fare are for specific, pre-known
10
13056
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var boldLink=document.getElementById('cmtbold');
2
4947
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept that connection and get the socket. I try to signal an event when this happens and pass that new socket object when the event happens, but when I try to pass the socket with this statement: ConnectionReceived(this, work); I get this exception in...
13
3244
by: ElderGeek | last post by:
Hello. I'm pretty new to C, and having a problem which I suspect this has to do with how I'm handling (or not handling) pointers. I have an array of dirent structures which I am passing to a function. The function fills the structures with information on subdirectories in a given directory. In the function itself, I can step through the array of structures and print to the console the list of directory names. However, when I try to do...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.