469,360 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

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 30316
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bjoern Wolfgardt | last post: by
3 posts views Thread by daveland | last post: by
1 post views Thread by andrewkooi | last post: by
31 posts views Thread by Royal Denning | last post: by
2 posts views Thread by Mike | last post: by
3 posts views Thread by Michael_R_Banks | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.