krishna_v
krishna_v

Reputation: 1511

parentNode.rowIndex not working in chrome

I am adding listeners to the cells of the table so that i can get the cell index when a particular cell is clicked. However i am unable to get the value of row index in chrome. This works fine in IE10 and Firefox. The code is:

function AttachEvents() {
    var cls = document.getElementById("TableContents").getElementsByTagName("td");

    for ( var i = 0; i < cls.length; i++ ) {
        if ( cls[i].addEventListener ) {
            cls[i].addEventListener("click", alertRowCell, false);
        } else if ( cls[i].attachEvent ) {
            cls[i].attachEvent("onclick", alertRowCell);
        }
    }
}

function alertRowCell(e) {
    var cell = e.target || window.event.srcElement;
    //alert( cell.cellIndex + ' : ' + cell.parentNode.rowIndex );
    if (cell.cellIndex == 1) {
        alert(" The row index is " + cell.parentNode.rowIndex);
        highlight();
    }
}

How this can be resolved in Chrome?

Upvotes: 0

Views: 1830

Answers (1)

Oleg
Oleg

Reputation: 9359

You can easily refer to the clicked element with this.

In your case:

function alertRowCell(evt) {
    var tr = this.parentNode;
    // do something with tr...
}

The reason I'm saying it is because if your table structure is...

<tr>
    <td><span>Hello</span></td>
</tr>

... then e.target is span whose parentNode is td and not tr.

Try this example: http://jsfiddle.net/naJBq/

Upvotes: 3

Related Questions