Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Table Accordian

Posted on 2011-03-17
2
Medium Priority
?
291 Views
Last Modified: 2012-05-11
The code below does what I want to expand child rows under parent rows.  I would like to add to it the ability to click an expanded parent row to collapse children underneath it.  Right now if I click on something already expanded it just does the same thing all the time.  Hope that was clear, if not please ask.

<html>
<head>
<title></title>
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
<script type="text/javascript">
	$(document).ready(function() {
		$('.child').hide();

		$('.parent').click(function(){
			$('.child').hide();
			$(this).nextUntil('.parent').fadeIn('normal');

		});

	});
</script>
</head>
<body>

<p/>

<table border='1'>
	<tr class="parent">
		<td>
			Parent
		</td>
	</tr>
	<tr class="child">
		<td>
			Child
		</td>
	<tr>
	<tr class="child">
			<td>
				Child
			</td>
	<tr>
	<tr class="parent">
			<td>
				Parent
			</td>
	</tr>
		<tr class="child">
			<td>
				Child
			</td>
		<tr>
		<tr class="child">
				<td>
					Child
				</td>
	<tr>
</table>
</body>
</html>

Open in new window

0
Comment
Question by:kyleroi
2 Comments
 
LVL 8

Accepted Solution

by:
McNetic earned 2000 total points
ID: 35164321
Change your click method to the following:
                $('.parent').click(function(){
                        var next = $(this).nextUntil('.parent', '.child');
                        if (next.is(':visible')) {
                          next.hide();
                        } else {
                          $('.child').hide();
                          next.fadeIn('normal');
                        }
                });

Open in new window

This will check if the items are visible, if yes hide them, if no hide all others and show the new ones.
0
 
LVL 1

Author Closing Comment

by:kyleroi
ID: 35177956
Works great thanks!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question