473,466 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[Beginner] Associating event handlers with elements

Hi, this is my first posting here. I also have a question about etiquette.
There's a html file associated with my question. I cannot host it
indefinitely at the current location. I don't, however, want to reproduce
it here, since it is a little unwieldy for that.

So, is it poor etiquette to refer to external files in postings, since the
postings will be archived, but the external files are not permanent?
Should I find a permanent home for further files that I may need with
further questions?

Someone might find this posting from the archives in N years to come,
after all.

* * *

Now, on to my question!

I want to build a table that knows where it has been clicked. I found the
following solution myself. Are there better ones?

http://www.student.oulu.fi/~pkarjala/question1.html
(tested on Mozilla 1.7.8/Linux)

It sets the event handlers for each TD in the build() loop, including a
parameter in the function call that is different for each TD.

The whole thing is a simple example with a table with 5 by 5 cells. If you
click on a cell it's supposed to change color. Naturally, my question is
one more general terms. How to make big tables that associate various
event handlers with various cells, and where you will know exactly which
element triggered the event? How to make it simple and maintainable?

--
Pekka Karjalainen - Oulu, Finland
Aug 9 '05 #1
4 1570
Are you trying to figure out which cell in the table was clicked or
were you trying to find the exact x, y coordinates of where the mouse
clicked in the table?

Aug 9 '05 #2

There are a couple of ways of binding event listeners:

OBJECT.EVENTTOLISTEN=HANDLERWITHNOARGUMENTS;
or DOM
OBJECT.addEventListener(sEVENT,oHANDLERWITHNOARGUM ENTS);
or for IE's
OBJECT.attachEvent(sEVENT,oHANDLERWITHNOARGUMENTS) ;
where HANDLERWITHNOARGUMENTS is usually a function called

Now, tables have a cells[] collection you can access and a rows[]
collection, you can iterate through either or both, and each node has its
own childNodes[] collection you can also iterate through doing the event
binding.

a 3rd technique is using a listener on the parent element and checked the
bubbled object

document.onclick=something;

function something(ev) {
// in IE the evented node will be event.srcElement
// in mozilla/opera(opera uses either target or srcElement) the node is
ev.target
alert(ev.target.nodeName) // will show the element tagName, so, if you
click on a cell, it'll be 'TD'

}

Using the 3rd tecnique, you set the event listening ONCE and check the
child that was evented and process accordingly, without having to bind a
handler/listener to each on a loop.

Danny

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Aug 9 '05 #3
On Tue, 9 Aug 2005 14:57:55 +0000 (UTC), Pekka Karjalainen
<pk******@mail.student.oulu.fi> wrote:
So, is it poor etiquette to refer to external files in postings, since the
postings will be archived, but the external files are not permanent?
Should I find a permanent home for further files that I may need with
further questions?
If you've got a permanent home then all the better, but there's
nothing particularly wrong with just linking to the file even if you
can't guarantee it's permanent, I certainly wouldn't use it to stop
you asking, or pasting too much in the post!
I want to build a table that knows where it has been clicked. I found the
following solution myself. Are there better ones?

http://www.student.oulu.fi/~pkarjala/question1.html
(tested on Mozilla 1.7.8/Linux)

It sets the event handlers for each TD in the build() loop, including a
parameter in the function call that is different for each TD.
createAttribute is not a good way to create event handlers.

x.onclick=functionRef

is much better, it's debateable if createAttribute should even create
an event handler...
The whole thing is a simple example with a table with 5 by 5 cells. If you
click on a cell it's supposed to change color. Naturally, my question is
one more general terms.


It would probably just be better to catch a single click at a higher
level, and then see which element was actually clicked...

Jim.
Aug 9 '05 #4
Markis Taylor asked:
Are you trying to figure out which cell in the table was clicked or
were you trying to find the exact x, y coordinates of where the mouse
clicked in the table?
No, I don't need the exact coordinates. I'd just like to divvy up a
larger element into components and know which one is being manipulated.

Jim Ley wrote: If you've got a permanent home then all the better, but there's
nothing particularly wrong with just linking to the file even if you
can't guarantee it's permanent, I certainly wouldn't use it to stop
you asking, or pasting too much in the post!
As long as my questions are really basic I doubt anybody will miss
seeing my files afterwards, so I won't worry about it now. I might only
need to know a little bit of JAvaScript after all. This thing I posted
about might be useful in prototyping something that I'll then rewrite in
something else.
createAttribute is not a good way to create event handlers.

x.onclick=functionRef

is much better, it's debateable if createAttribute should even create
an event handler...

Thanks for the advice. I need to read my references more carefully to
spot basics like x.onclick!
It would probably just be better to catch a single click at a higher
level, and then see which element was actually clicked...

Can you give an example or a simple outline of how to implement this
cleanly in Javascript? Doesn't this involve manipulating coordinates or
something similar? I'd like a solution that does not require special
knowledge of how big things are (in pixels), and probably I'm not just
seeing what you are suggesting properly.

Thanks for the responses.
--
Pekka Karjalainen - Oulu, Finland
Aug 10 '05 #5

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

Similar topics

4
by: TempMan | last post by:
I want this text field to always display a number variable. The variable "num" is defined in the head, how can I get a text box to display this varibale?? <input name="Balance" type="text"...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
4
by: News | last post by:
I have a page with many controls. Among these controls there is a table which is a datagrid with nested repeater inside. My problem is that I can not use DataGridCommandEventArgs to get datagrid...
1
by: Novice | last post by:
Hey all, I realize this must be a fairly common requirement in ASP.NET but I have yet to come across the right way to do it. Basically, right now I have a single asp:label on my webpage and I am...
5
by: mayur_hirpara | last post by:
Hi, I have been developing web applications for a while now. However, as I was thinking through the architecture I really don't understand the "How server can identify between which buttons has...
17
by: dan_williams | last post by:
I have the following test web page:- <html> <head><title>Test</title> <script language="Javascript"> <!-- function fnTR() { alert("TR"); }
2
by: niks | last post by:
Is there a standard way of traversing the DOM to find all the javascript in a document? As far as I know, the only legal positions for javascript in the DOM is within a <scriptelement or in the...
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
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
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
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...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.