Reputation: 24562
I have a table where my rows have classes:
<table id="abc">
<tr class="level-0">
<tr class="level-1">
<tr class="level-2">
</table>
I need to create two buttons.
Can someone tell me how I can implement this with jQuery?
Upvotes: 2
Views: 18486
Reputation: 44
I have same problem where next level is child of previous tr.
`<table>
<tbody>
<tr class="level-1"></tr>
<tr class="level-2"></tr>
<tr class="level-3"></tr>
<tr class="level-4"></tr>
<tr class="level-4"></tr>
<tr class="level-2"></tr>
<tr class="level-2"></tr>
</tbody>
</table>
So far i have
$('table tbody .level-1').click(function(event) {
event.preventDefault();
var current_tr_class = $(this).parent().attr('class');
next_tr_class = parseInt(current_tr_class.replace ( /[^\d.]/g, '' ))+1; // get last increase by 1 it is 1 make it 2
$('tr.level-'+next_tr_class).toggle({ opacity: "toggle" }); // Open toggle2
});
I hope this could solve your problem but not mine i have all dynamically generated. and if you click on level-1 it will untoggle all the element with level-2 even if it is after level-4. but level-4 will be still hide and it will be like loop.
Upvotes: 0
Reputation: 10896
Check this one
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" >
</script>
<script type="text/javascript">
$(function(){
$('#level_1_2').click(function(){
$('.level-2').toggle();
});
$('#level_2').click(function(){
if(jQuery('.level-1').is(':visible')){
$('.level-2').show();
}else{
$('.level-2').hide();
}
$('.level-2 , .level-1').toggle();
});
});
</script>
</head>
<body>
<input type="button" id="level_1_2" value="Hide level 1 and 2">
<input type="button" id="level_2" value="Hide level 2">
<table id="abc">
<tr class="level-0"><td>0</td></tr>
<tr class="level-1"><td>1</td></tr>
<tr class="level-2"><td>2</td></tr>
</table>
</body>
</html>
Upvotes: 0
Reputation: 5588
$(function(){
$('#button1').click(function(){
$('#level0').hide('slow');
$('#level0').hide('slow');
...
});
$('#button2').click(function(){
$('#level0').show('slow');
$('#level0').show('slow');
...
});
});
Upvotes: 0
Reputation: 27584
Use click
event handler and toggle
function (for description check jquery API):
$('#button1').click(function() {
// all trs with level-1 class inside abc table
$('#abc tr.level-1').toggle();
});
$('#button1and2').click(function() {
// all trs with level-1 or level-2 class inside abc table
$('#abc tr.level-1, #abc tr.level-2').toggle();
});
Upvotes: 5
Reputation: 7056
Use a click and the next()
function:
$('.level-1').click(function(){
$(this).next('.level-2').toggle();
});
Upvotes: 0