473,604 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

table and onClick

Hi all!

I have a html-document with a table. How can I recognise in the onClick
event-handler, that I which cell of a table clicked?

P.
Apr 1 '06 #1
6 30564
You have to set the onclick event of the TD tags, not the TABLE itself.
You can do this in a loop, for efficiency:

var myTblCells =
document.getEle mentById("myTab le").getElement sByTagName("td" );
for (var t=0,curCell; curCell=myTblCe lls[t]; t++)
curCell.onclick = someFunction;

The above code is IE-specific, of course -- you'll want to generalize
it to support modern browsers. The someFunction code tells you where
you are:

function someFunction()
{
alert("I'm in row " + this.rowIndex + ", cell " + this.cellIndex) ;
}

Hope this helps,

Stan Scott
New York City

Apr 1 '06 #2
Thanks!

P.
Apr 1 '06 #3
B. P. TBC wrote:
I have a html-document with a table. How can I recognise in the onClick
event-handler, that I which cell of a table clicked?


Use Event Bubbling.
<URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-flow-bubbling>

<script type="text/javascript">
function handleClick(e)
{
var t;
if (e && ((t = e.target) || (t = e.srcElement)))
{
... t ...
}
}
</script>

<table onclick="handle Click(event);">
...
</table>
PointedEars
Apr 1 '06 #4
st*****@gmail.c om wrote:
You have to set the onclick event of the TD tags, not the TABLE itself.
Not at all.
You can do this in a loop, for efficiency:

var myTblCells =
document.getEle mentById("myTab le").getElement sByTagName("td" );
for (var t=0,curCell; curCell=myTblCe lls[t]; t++)
curCell.onclick = someFunction;

The above code is IE-specific, of course [...]


It is not, and it is unnecessary. The `click' event bubbles by default
in all implementations .
PointedEars
Apr 1 '06 #5
Thomas 'PointedEars' Lahn wrote:
B. P. TBC wrote:

I have a html-document with a table. How can I recognise in the onClick
event-handler, that I which cell of a table clicked?

Use Event Bubbling.
<URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-flow-bubbling>

<script type="text/javascript">
function handleClick(e)
{
var t;


var e = e || window.event;
if (e && ((t = e.target) || (t = e.srcElement)))
{
... t ...
}
}
</script>

<table onclick="handle Click(event);">
...
</table>
PointedEars

--
Rob
Apr 2 '06 #6
RobG wrote:
Thomas 'PointedEars' Lahn wrote:
B. P. TBC wrote:
I have a html-document with a table. How can I recognise in the onClick
event-handler, that I which cell of a table clicked?


Use Event Bubbling.
<URL:http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-flow-bubbling>
<script type="text/javascript">
function handleClick(e)
{
var t;


var e = e || window.event;


Unnecessary:
if (e && ((t = e.target) || (t = e.srcElement)))
{
... t ...
}
}
</script>

<table onclick="handle Click(event);"> ^^^^^ ...
</table>

PointedEars
Apr 2 '06 #7

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

Similar topics

3
16506
by: Bjoern Wolfgardt | last post by:
Hi, I am a little bit confused. Maybe someone can explain what I am doing wrong. I have a table with 2 cells in it. In the first cell I have an image. The image is left aligned and centered. In the second cell I also put an image. It is also left aligned and centered. It works wonderfull. But now I add some Text to the second cell. Baff - the valign is broken.
3
3149
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a row has been added is itself contained within an outer table (to handle the desired screen layout). That outer table does not properly grow to contain the new size of the table to which the row was added. (More specifically, I have...
1
2007
by: andrewkooi | last post by:
Greetings, I have 16 links for my horizontal menu navigation as you can see in my website www.singakad.com and at present, I am using a table and javascript for this menu. Is it possible for me to create a submenu for certain cells in this horizontal menu? How do I go about doing this? Thank you in advance for any assistance given.
31
6956
by: Royal Denning | last post by:
I am designing a table with 2 columns and 20 rows. I want to insert small images (each with a link) and a text title in each cell of the table. On refresh of the page, I would like to have the contents of each cell (picture/link and associated text) rotate randomly. Note that the text titles must remain with their associated picture and link (I don't want to scramble all the elements--just rotate the entire contents of the table cell).
2
11511
by: Mike | last post by:
My page populates a table with a list of names and other information from a JavaScript object. I receive changes (adds, change & delete) to that list, convert it into a JavaScript object. I do understand how to add the new names to the list. I have 3 questions: 1) How can I search through the table rows to find the rows to be changed or removed?
6
9962
by: anirban.anirbanju | last post by:
hi there, i've some serious problem to add rows dynamically in a table. my table contains 5 cell. | check | from_value | to_value | color_text | color_value | --------------------------------------------------------------------------------- | | | | | | | | | | |
3
3987
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table with a createElement function to add additional entries. The table has the first row of input text boxes already in it. You have to click a button to add another row. That seems to be working fine. How do I pull the information from the input boxes...
3
6485
by: Michael_R_Banks | last post by:
I'm trying to dynamically build a table that allows users to remove rows when they click a corresponding button. For some reason, whenever I add the button to the table, it never fires the onclick event. I'm stumped with this one, any assistance would be appreciated. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"...
2
2166
by: querry | last post by:
Hi all, I am trying to change the background color of individual Table Cells using the javascript as follows. function changecolor(e) { var etarg; if (!ee) var ee = window.event; if (ee.target) etarg = ee.target; else if (ee.srcElement) etarg = ee.srcElement;
4
4750
iam_clint
by: iam_clint | last post by:
This is one way to make it possible to highlight rows and cols in a table with an intersecting color <script> /* Javascript written by iam_clint */ vertColor="#66ffcc"; horzColor="#b7efff"; intersectColor="#aa22f0"; function vertClick(col, tbl) { var color="";
0
8419
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
8406
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
8065
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
6737
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...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3906
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3954
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2433
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 we have to send another system
1
1522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.