views:

2169

answers:

4

Hey! I'm trying to add an onclick event to a table row through Javascript.

function addRowHandlers() {
    var table = document.getElementById("tableId");
    var rows = table.getElementsByTagName("tr");
    for (i = 1; i < rows.length; i++) {
        row = table.rows[i];
        row.onclick = function(){
                          var cell = this.getElementsByTagName("td")[0];
                          var id = cell.innerHTML;
                          alert("id:" + id);
                      };
    }
}

This works as expected in Firefox, but in Internet Explorer (IE8) I can't access the table cells. I believe that is somehow related to the fact that "this" in the onclick function is identified as "Window" instead of "Table" (or something like that).

If I could access the the current row I could perform a getElementById in the onclick function by I can't find a way to do that. Any suggestions?

Thanks!

+1  A: 
SolutionYogi
That doesn't work because of this http://stackoverflow.com/questions/454517/javascript-onclick-problem-with-table-rows
lolpatlol
Are you sure? It does work. Check the working demo link in my updated answer. Also, in your code, you started index with 1 instead of 0. You will not attach handler to the first row.
SolutionYogi
It works but when I saw your solution it was a bit different than it is now... =P I'm really thankful, been banging my head all afternoon!
lolpatlol
Yes, I realized it right after I posted my answer (just like redsquare did) and later corrected it.
SolutionYogi
+1  A: 

Head stuck in jq for too long. This will work.

function addRowHandlers() {
    var table = document.getElementById("tableId");
    var rows = table.getElementsByTagName("tr");
    for (i = 1; i < rows.length; i++) {
        var row = table.rows[i];
        row.onclick = function(myrow){
                          return function() { 
                             var cell = myrow.getElementsByTagName("td")[0];
                             var id = cell.innerHTML;
                             alert("id:" + id);
                      };
                  }(row);
    }
}
redsquare
Would this work? I posted the same example but I think the 'row' inside the onlcick will always refer to the last row. Isn't it? [JavaScript has only function scoping, no block scoping.]
SolutionYogi
Yeah that doesn't work because of scoping, row is a global variable..
lolpatlol
I know, jQuery makes all these things so easy, when you are left without jQuery, you do all the silly mistakes! :)
SolutionYogi
indeed it does! give me an each anyday
redsquare
A: 

I think for IE you will need to use the srcElement property of the Event object. if jQuery is an option for you, you may want to consider using it - as it abstracts most browser differences for you. Example jQuery:

$("#tableId tr").click(function() {
   alert($(this).children("td").html());
});
Nick Riggs
A: 

Try changing the this.getElementsByTagName("td")[0]) line to read row.getElementsByTagName("td")[0];. That should capture the row reference in a closure, and it should work as expected.

Edit: The above is wrong, since row is a global variable -- as others have said, allocate a new variable and then use THAT in the closure.

Luke Rinard
That wouldn't work. row will only refer to the last row.
SolutionYogi
You're absolutely right, I didn't notice row was a global.
Luke Rinard