Reputation: 616
I am trying to trigger an event when I click on a td
with class toggle
. When I click on a td with class toggle
, I want to show all the next tr
with class hidethis
.
But I cant seem to trigger an event on the td
. Here's what I have got so far.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> </script>
<script type="text/javascript">
$(document).ready(function () {
$(' td .toggle ').on('click',function(ev){
$(this).closest('tr').nextAll('tr .hidethis').show();
});
});
</script>
</head>
<body>
<table border="1">
<tr >
<td class="toggle">First Toggle</td>
</tr>
<tr class = "hidethis" style="display:none">
<td>Value 1</td>
<td>Value 2 </td>
<td>Value 3</td>
</tr>
<tr >
<td class="toggle">Second Toggle</td>
</tr>
<tr class = "hidethis" style="display:none">
<td>Value 4</td>
<td>Value 5 </td>
<td>Value 6</td>
</tr>
</table>
</body>
</html>
The problem is when I click on the td
with class toggle
the event is not firing.
Please help me. May be I am not seeing something obvious.
Thanks in advance.
Upvotes: 1
Views: 7627
Reputation: 28763
Simply try like
$(document).ready(function () {
$('.toggle').on('click',function(ev){
$(this).closest('tr').nextAll('tr .hidethis').show();
});
});
Or remove the space between the td
and its classname
$('td.toggle').on('click',function(ev){
Upvotes: 1
Reputation: 7762
Remove space between td and toggle
$(' td .toggle ').on('click',function(ev){
to
$('td.toggle').on('click',function(ev){
^
and also same problem with your below code:-
$(this).closest('tr').nextAll('tr .hidethis').show();
to
$(this).closest('tr').nextAll('tr.hidethis').show();
^
Upvotes: 1
Reputation: 816282
Remove the space between the selectors, it has a specific meaning:
Selects all elements that are descendants of a given ancestor.
It should be:
$('td.toggle')
Upvotes: 1