views:

49

answers:

3

i want to get a event that will fire when i click inside a td of a html table

i had this:

 ('td').live('click', function() {
            alert($(this).attr('id'));
        });

which works but this fires inside the 'th" cells as well (not sure why).

is there any selector that just fires inside td's and not th's. I tried this:

 ('tbody td').live('click', function() {
            alert($(this).attr('id'));
        });

but that didn't seems to stop this firing for the th's.

EDIT:

I figured out the issue.. the problem is that the whole table was inside another table so even the "th" was actually inside a "td" of a larger table.

The solution was to do this:

 $('table.calendar td').live('click', function() {
            alert($(this).attr('id'));
        });
A: 

If it's not done already, put your th in thead and your td in tbody :

<table>
  <thead>
    <tr>
      <th>my</th><th>title</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>my</td><td>content</td>
    </tr>
  </tbody>
</table>

Does this help ?

Pierre Henry
@Pierre Henry - th are already inside of thead
ooo
Why has someone given this a down vote? It seems like a totally reasonable response to me even if it doesn't fix the problem!!!
MarkB29
@Pierre Henry - problem fixed. see above
ooo
@MarkB : I agree ! The html was not provided, I could not know...
Pierre Henry
I just removed down-votes for both the minused answers here. I agree, I don't think it's helpful to downvote, especially without explanation, when the intention of an answer is to aid in tracking down the problem.
karim79
@Karim thanks :)
Pierre Henry
+2  A: 

Your code works for me. What does your table look like?

Bradley Mountford
@Bradley Mountford - problem fixed (see above)
ooo
A: 

I figured out the issue.. the problem is that the whole table was inside another table so even the "th" was actually inside a "td" of a larger table.

The solution was to do this:

$('table.calendar td').live('click', function() { alert($(this).attr('id')); });

ooo