views:

30

answers:

1

I have two tables and I want to have change event on one table. That is when I click on first table, I want to have some changes to the second table. But somehow below change function is not working. Any suggestions?

$("#history_table table:eq(0)").click(function(){
                    $("#history_table table:eq(1) thead tr th:nth-child(1)").html("New");
                }); 

$("#history_table table:eq(1) thead tr th:nth-child(1)").change(function() {
                      alert('Handler for .change() called.');
                });

Here history_table is a div tag id and it has two tables. click() function is working but I am expecting to see alert message as well, but this is not showing.

+4  A: 

The .change() event/handler isn't for (or fired by default for) this, it's for an input style element changing data, for example a <input> or a <select>. Instead, you'll want a custom event (or manually firing change...) here, for example:

$("#history_table table:eq(0)").click(function(){
  $("#history_table table:eq(1) thead tr th:nth-child(1)").html("New")
     .trigger('myEvent');
}); 

$("#history_table table:eq(1) thead tr th:nth-child(1)").bind('myEvent', function() {
  alert('Handler for .change() called.');
});
Nick Craver
Thanks Nick, it works fine. But what If I want to have my second table to always listen to an event, that is instead of calling trigger from another function, automatically I want to display alert message when somebody changes second table values. Is there any way?
goutham
@goutham - Not a built-in cross-browser one nope...that'd be very expensive to monitor :)
Nick Craver