473,385 Members | 1,893 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,385 software developers and data experts.

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 30551
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.getElementById("myTable").getElementsByTa gName("td");
for (var t=0,curCell; curCell=myTblCells[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#Events-flow-bubbling>

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

<table onclick="handleClick(event);">
...
</table>
PointedEars
Apr 1 '06 #4
st*****@gmail.com 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.getElementById("myTable").getElementsByTa gName("td");
for (var t=0,curCell; curCell=myTblCells[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#Events-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="handleClick(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#Events-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="handleClick(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
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....
3
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...
1
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...
31
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...
2
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...
6
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
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...
3
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...
2
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; ...
4
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";...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...

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.