Solved

Table Accordian

Posted on 2011-03-17
2
278 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 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now