Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How Can I able to Collapse and Expand
Table rows On click
Have Tried like Below
and is working too
But I want that onLoad of the Table
It must be in collapsed state.
and only when the user clicks,it should Expand.

But this Code is not working as ,its loading
with rows Expanded.But I need to get Collapsed.When the Page Loads.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Untitled Page
</title>

<script type="text/javascript">

function poorman_toggle(id)
{
var tr = document.getElementById(id);
if (tr==null) { return; }
var bExpand = tr.style.display == '';
tr.style.display = (bExpand ? 'none' : '');
}
function poorman_changeimage(id, sMinus, sPlus)
{
var img = document.getElementById(id);
if (img!=null)
{
var bExpand = img.src.indexOf(sPlus) >= 0;
if (!bExpand)
img.src = sPlus;
else
img.src = sMinus;
}
}

function Toggle_trGrpHeader2()
{
poorman_changeimage('trGrpHeader2_Img', 'images/minus.gif', 'images/plus.gif');
poorman_toggle('row1');
poorman_toggle('row2');
poorman_toggle('row3');
}

function Toggle_trGrpHeader1()
{
poorman_changeimage('trGrpHeader1_Img', 'images/minus.gif', 'images/plus.gif');
poorman_toggle('trRow1');
}
</script>

</head>
<body>

<div>
<table border="1">
<tr id="trGrpHeader1">
<td colspan="4"><span  önclick="javascript:Toggle_trGrpHeader1();"> header for row 1</span></td>
</tr>

<tr id="trRow1">
<td>  Hello</td>
<td class="number">10</td>
<td class="number">1999</td>
<td class="number">2000</td>
</tr>

<tr id="trGrpHeader2">
<td colspan="4"><span  önclick="javascript:Toggle_trGrpHeader2();">header for row 2</span></td>
</tr>

<tr id="row1">
<td>  Hello2</td>
<td class="number">10743</td>
<td class="number">1998</td>
<td class="number">1997</td>
</tr>
<tr id="row2">
<td>  Hello3</td>
<td class="number">10768</td>
<td class="number">2012</td>
<td class="number">2011</td>
</tr>
<tr id="row3">
<td>  Hello4</td>
<td class="number">10793</td>
<td class="number">1995</td>
<td class="number">1993</td>
</tr>

</table>
</div>
</body>
</html>

Please assist
To achieve This.
I just want that on load all the rows must be collapsed.
Posted

1 solution

I think you can achieve this by using jquary I'll post a sample solution and I hope it will helps you.

HTML
<html>
	<head>
		<title>jQuary Base Menu</title>
		<script type="text/javascript" src="jquery.js"></script>		
		<script type="text/javascript">
			$(document).ready(function(){
				$("#m1d").hide();
				$("#m2d").hide();
				$("#m3d").hide();
				
				$("#m1").click(function(){					
					$("#m1d").show();
					$("#m2d").hide();
					$("#m3d").hide();
				  });
				$("#m2").click(function(){					
					$("#m1d").hide();
					$("#m2d").show();
					$("#m3d").hide();
				  });
				$("#m3").click(function(){					
					$("#m1d").hide();
					$("#m2d").hide();
					$("#m3d").show();
				  });
			});
		</script>
		
		<style type="text/css">
			.mainmune{
				text-align:center;
				color:#99FF00;
				background-color:#787878;
				font-family:Verdana;
				font-size:16px;
				font-weight:bold;
				height:35px;
				border:5px solid #FFFF00;
			}
			.submune{
				text-align:left;
				color:#FFFF00;
				background-color:#989898;
				font-family:Verdana;
				font-size:14px;
				font-weight:bold;
				margin-left:50px;
			}
		</style>
	
	</head>
	<body>
		<table border="0" width="450px" removed="#787878" cellspacing="1" cellpadding="0">
			<tr id="m1" class="mainmune">
				<td>Menu 1</td>
			</tr>
			<tr id="m1d" class="submune">
				<td>
					Sub Menu 1<br>
					Sub Menu 2
				</br></td>				
			</tr>
			<tr id="m2" class="mainmune">
				<td>Menu 2</td>
			</tr>
			<tr id="m2d" class="submune">
				<td>
					Sub Menu 3<br>
					Sub Menu 4
				</br></td>				
			</tr>
			<tr id="m3" class="mainmune">
				<td>Menu 3</td>
			</tr>
			<tr id="m3d" class="submune">
				<td>
					Sub Menu 5<br>
					Sub Menu 6
				</br></td>				
			</tr>			
		</table>
	
	</body>

</html>


For run this you need to download jquery.js file from jQuery website http://jquery.com/

I hope this will helps you....
Thanks
 
Share this answer
 
v2
Comments
Member 11396618 23-Jan-15 4:27am    
I want to expand the particular table data (td) and display some .log file. your code is not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900