Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > table and onClick

Reply
Thread Tools

table and onClick

 
 
B. P. TBC
Guest
Posts: n/a
 
      04-01-2006
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.


 
Reply With Quote
 
 
 
 
stannyc@gmail.com
Guest
Posts: n/a
 
      04-01-2006
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

 
Reply With Quote
 
 
 
 
B. P. TBC
Guest
Posts: n/a
 
      04-01-2006
Thanks!

P.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-01-2006
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      04-02-2006
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-02-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding an onclick with another onclick tomlong@gmail.com Javascript 4 01-26-2006 09:26 PM
button.onclick = new Function("func2()") + button.onclick foldface@yahoo.co.uk Javascript 2 09-26-2005 08:13 AM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
document.onclick=doIt() same as document.onclick=doIt ? bob Javascript 3 08-21-2003 12:14 PM
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table. David Williams ASP .Net 2 08-12-2003 07:55 AM



Advertisments