Reputation: 101
Let's say this is my table:
<table>
<tr>
<td>a</td>
<td>one</td>
<td>two</td>
</tr>
<tr>
<td>b</td>
<td>three</td>
<td>four</td>
</tr>
<tr>
<td>c</td>
<td>five</td>
<td>six</td>
<table>
Using Javascript, I want to be able to toggle the display: none
element of the rows below the one I click on... but only if I click inside the first cell in each row.
For example, if I clicked on the cell containing a
, the tr
containing the cells b
, three
, and four
and the tr
containing the cells c
, five
, and six
wouldn't display until I clicked it again. Clicking one
or two
would not do anything, though.
The table I am going to be using this code on is much larger, though.
Upvotes: 1
Views: 203
Reputation: 14782
Using jQuery:
jQuery('table').find('tr').find('td:first').click(function() {
jQuery(this).parent().next().toggle();
});
Upvotes: 0
Reputation: 9027
With jQuery, it's really easy:
$("table tr td:first-child").click(function() {
$(this).parent().nextAll().toggle();
});
I'll update my answer with a plain JavaScript answer if i find one.
Upvotes: 1