Create a Fast and Simple Toggle View Content with jQuery

Demonstration Download

Introduction

This would be a simple, quick and easy tutorial. The reason I make this tutorial is for my upcoming post which will show you the plugins and scripts that I used in my most recent project. It's going to be a really useful post and make sure you won't miss it :)

This script is fairly straight forward. What it does is using the UL list and allow user to toggle to display the content of LI items. This is a really useful user interface feature that it helps designers to save space and build a less complicated user interface. It eliminates redundant element and allow user view the content only when they want to see it.

And...... this is how it will look like:

jQuery Toggle View

1. HTML

It consists of UL list and the rest is quite self-explanatory.

<ul id="toggle-view">
	<li>
		<h3>Title 1</h3>
		<span>+</span>
		<div class="panel">
			<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim.</p>
		</div>
	</li>
	<li>
		<h3>Title 2</h3>
		<span>+</span>
		<div class="panel">
			<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim.</p>
		</div>
	</li>
	<li>
		<h3>Title 3</h3>
		<span>+</span>
		<div class="panel">
			<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim.</p>
		</div>
	</li>
</ul>

2. CSS

I used the minimum CSS code to style this tutorial. The most important thing is, we have to make sure the P is hidden by default.

#toggle-view {
	list-style:none;	
	font-family:arial;
	font-size:11px;
	margin:0;
	padding:0;
	width:300px;
}

	#toggle-view li {
		margin:10px;
		border-bottom:1px solid #ccc;
		position:relative;
		cursor:pointer;
	}
	
	#toggle-view h3 {
		margin:0;
		font-size:14px;
	}

	#toggle-view span {
		position:absolute;
		right:5px; top:0;
		color:#ccc;
		font-size:13px;
	}
	
	#toggle-view .panel {
		margin:5px 0;
		display:none;
	}	

3. Javascript

This would be one of my shortest jQuery code! We attach click event to the LI item and everytime a user click on the item check if P tag is visible. If it's hidden, show it otherwise hide it. Also, it will change the html value for the SPAN to either plus or negative sign.

$(document).ready(function () {
	
	$('#toggle-view li').click(function () {

		var text = $(this).children('div.panel');

		if (text.is(':hidden')) {
			text.slideDown('200');
			$(this).children('span').html('-');		
		} else {
			text.slideUp('200');
			$(this).children('span').html('+');		
		}
		
	});

});

Conclusion

I hope you will like this quick and short tutorial! :) stay tuned for my upcoming post!

Demonstration Download

AdvertisementIncredible online 640-816 dumps training programs & BCP-420 help you in pass 640-553 exam regarding 642-617. We offer best quality 650-987 training tools for your success.

Show Some Love, Spread This Post!

26 comments

lauren Thu, 19th April 2012 how do i make the span AND the h3 toggle it similar to what xh0b0tx was trying to do?
Reply
lauren Thu, 12th April 2012 I was able to make the h3 titles become where the user clicks to close and open like user xh0b0tx wanted, but I also want that h3 text to switch text when the user opens and closes the content.

I tried changing both
('span')

to

('h3')
with the wording I wanted to use but that didn't work

can you help?
Reply
Phil Tue, 6th March 2012 anyone else having problems in ie displaying images? It works fine in firefox but when tested in IE the images dissapear. Also is it possible to display tables?
Reply
Kevin Liew Tue, 6th March 2012 You're trying to display more HTML stuff in this list.. you should change this to a div:

var text = $(this).children('p');
to
var text = $(this).children('div.panel');


HTML:
<li>
<h3>Title 3</h3>

<span>+</span>
<div class="panel"><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim.</p>
</div>
</li>
Tam Fri, 23rd March 2012 I tried this ... but it doesn't work (it doesn't expand content). Any idea? I am using Chrome btw.
Kevin Liew Mon, 26th March 2012 hey guys, I have updated the tutorial, now you can put whatever thing you like in there. :)
run Sat, 3rd March 2012 thanks for the tutorial
Reply
Sceptra Thu, 23rd February 2012 This script works great, however have an issue I use this to toggle email messages downloaded onto my database, however with the ones that have html coding, it doesn't work, is there a way to bypass it, I found out that the <div> tag and <br><hr style="HEIGHT: 1px"> tags are causing the problem, any help would be much appreciated
Reply
Rom Tue, 14th February 2012 Hi. I have some question. i need next option - first (or specified) li item was opened by default. tnx
Reply
Kevin Liew Tue, 14th February 2012 Try this, put it in the first line of $(document).ready().

$('#toggle-view li:eq(0)').children('p').show();
$('#toggle-view li:eq(0)').children('span').html('-');

use the eq(0) to define which item you want it to be opened.
rom Wed, 15th February 2012 tnx;)
Red Thu, 2nd February 2012 Is there a way to add an active class to the h3 so it's colour can change to show it's selected?
Reply
Kevin Liew Tue, 7th February 2012 Yes, mod the script from:

if (text.is(':hidden')) {
text.slideDown('200');
$(this).children('span').html('-');
$(this).children('h3').addClass('selected');
} else {
text.slideUp('200');
$(this).children('span').html('+');
$(this).children('h3').removeClass('selected');
}

H3 will get a class called "selected".
xh0b0tx Mon, 17th October 2011 I have some questions. When a put image or link in <li></li> and click on it, the section closed automaticly. It's not useful for me. I want close the element only if I click on <h3> header. How do I make it?
Reply
Kevin Liew Mon, 17th October 2011 It was a mistake also, you can change this:

$('#toggle-view li').click(function () ......

to

$('#toggle-view li h3').click(function () ......
xh0b0tx Tue, 18th October 2011 $(document).ready(function () {

$('#toggle-view li h3').click(function () {

var text = $(this).children('p');

if (text.is(':hidden')) {
text.slideDown('200');
$(this).children('span').html('-');
} else {
text.slideUp('200');
$(this).children('span').html('+');
}

});

});

I'm doing like you say but its not working. Element is not toggle up and down.
Kevin Liew Tue, 7th February 2012 $(document).ready(function () {

$('#toggle-view li h3').click(function () {

var text = $(this).siblings('p');

if (text.is(':hidden')) {
text.slideDown('200');
$(this).siblings('span').html('-');
} else {
text.slideUp('200');
$(this).siblings('span').html('+');
}

});

});
sunshine Thu, 10th June 2010 This is so awesome! So simple and so wonderful! THANKS!
Reply
a Mon, 24th May 2010 very nicee..tq :)
Reply
Kaushal Thu, 8th April 2010 When i open one item, it should automatically close the other open items.. how can this be done?
Reply
kevin Wed, 3rd February 2010 Hi Fred, yea, something the simplest thing is really really useful. I found myself using this technique very often to simplify the web interface and encourage user interactivity :)
Reply
Fred Rocha Wed, 3rd February 2010 Just implemented this toggle on our to be FAQ page. It's something I was looking out for some time: simple and effective.

Merci!
Reply
andi Thu, 21st January 2010 sorry, it works if you have 2 toggles in one page, i made a mistake in my CSS file.
Reply
andi Thu, 21st January 2010 hello, is it possible to have 2 or more such toggles in a single page?
i`ve tried to duplicate the js/html/css and change the DIV IDs for the second, but one of them always loads opened up, while the other one is closed by default. both work properly, the only problem is the open by default one; i`d like to have them both closed by default.
is this possible?

thanks & congrats for this simple and useful script!
andi
Reply
Boba Tue, 19th January 2010 Cool :)
Reply
qlworx internetagentur Tue, 19th January 2010 This is sweet. I will use it for a FAQ section. Bookmarked your site!
Reply

Leave a comment

Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.

Advertisement