One warning, Safari always returns 0 for cellIndex. You can get around
this in a number of ways which will all depend on what your needs are.

In a recent project I did something like this:

// for each of the relevant tables
$('table').each( function() {
        if ( this.rows[0].cells[1].cellIndex == 0 ) {
                $('tr', this).each( function() {
                        for ( var i = this.cells.length - 1; i >= 0; i-- )
                                this.cells[i].$cellIndex = i;
                });
        }
});


// to access the cellIndex of a TD
var thisCellIndex = this.$cellIndex || this.cellIndex;

Another option here:

   
http://scott-tabar-safari.blogspot.com/2006/07/safari-and-broken-javascript.html

Karl Rudd

On 6/20/07, Matt Conrad <[EMAIL PROTECTED]> wrote:

Yes, I did mean position as in row, column.

In fact the code I had was fine, except for one thing:  in the alert,
I was trying to use the event itself (e) rather than the target (t).
(I didn't understand the (e && ...) code.)

Changing to "alert(t.cellIndex  + ', ' + t.parentNode.rowIndex)" works
fine in FF and IE both.

Your suggestion started me on the right track, and
http://www.quirksmode.org/js/events_properties.html
took me the rest of the way.

Thanks,
Matt


On 6/19/07, Scott Sauyet <[EMAIL PROTECTED]> wrote:
>
> Matt Conrad wrote:
> > I'm trying to find the cleanest way to get the position (x, y) of a
> > table cell that is clicked.  I'll eventually post the coordinates back
> > to the server.
>
> By (x, y) you mean (row, column) right, not pixel measurements?  That's
> what the code seemed to attempt.
>
> Without a live example, I'm going to be simply guessing, but, hey,
> that's never bothered me before!  :-)
>
> > function handleClick(e)
> > {
> >   var t;
> >   if (e && ((t = e.target) || (t = e.srcElement)))
> >   {
> >     alert(e.target.cellIndex + ', ' + e.target.parentNode.rowIndex);
> >   }
> > }
>
> I'm guessing that the target in IE is not the TD you are on.  Perhaps
> it's a text node inside your TD.
>
> You might add something like this (untested):
>
>      if (!$(t).is("td")) {
>          t = $(t).parents("td")[0];
>      }
>
> That should, I believe, give you the first TD containing the cell you
> are interested in.
>
> You can see more about parents at
>
>      http://docs.jquery.com/DOM/Traversing#parents.28_expr_.29
>
>
> Good luck,
>
>    -- Scott
>
>

Reply via email to