Pligg CMS Forums


Go Back   Pligg CMS Forum > Pligg Development > Modification Tutorials



How to make a Digg-style FAQ page with JavaScript and jQuery

Advanced Tutorials for Modifying your Pligg site


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-2008, 06:20 PM
New Pligger
Pligg Version: 9.9.0
Pligg Template: default
 
Join Date: Jan 2008
Posts: 16
Post How to make a Digg-style FAQ page with JavaScript and jQuery

How to make a Digg-style FAQ page with JavaScript and jQuery

First take a look at the example page at Digg.com
Digg - Frequently Asked Questions

We will use the popular and brilliant JavaScript library, jQuery.
Digg is also using the same library.

First go ahead and download the latest version of jQuery here:
Downloading jQuery - jQuery JavaScript Library

Let's begin:

Here is a basic page:
HTML Code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Digg-style FAQ page</title>
</head>

<body>
</body>
</html>
Now let's include our jQuery library:
HTML Code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Digg-style FAQ page</title>
<script type="text/javascript" src="jquery.js"> </script>
</head>

<body>
</body>
</html>
Then we will add a few questions and answers to the page:
HTML Code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Digg-style FAQ page</title>
<script type="text/javascript" src="jquery.js"> </script>
</head>

<body>

   <a href="#" name="faq-1">What is Digg like FAQ?</a><br />
   <div class="faq-answer" id="faq-1">It's like this: http://www.digg.com/faq<br /><br /></div>
   
   <a href="#" name="faq-2">Is it easy to make?</a><br />
   <div class="faq-answer" id="faq-2">Yes it's very easy to make!<br /><br /></div>
   
   <a href="#" name="faq-3">Why would I want to do that?</a><br />
   <div class="faq-answer" id="faq-3">And your page will be so much cleaner.<br /><br /></div>
            
</body>
</html>
We need to hide the answers for now:
HTML Code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Digg-style FAQ page</title>

<script type="text/javascript" src="jquery.js"> </script>

<style type="text/css">
	.faq-answer {
	display:none;
	}
</style>

</head>

<body>

   <a href="#" name="faq-1">What is Digg like FAQ?</a><br />
   <div class="faq-answer" id="faq-1">It's like this: http://www.digg.com/faq<br /><br /></div>
   
   <a href="#" name="faq-2">Is it easy to make?</a><br />
   <div class="faq-answer" id="faq-2">Yes it's very easy to make!<br /><br /></div>
   
   <a href="#" name="faq-3">Why would I want to do that?</a><br />
   <div class="faq-answer" id="faq-3">And your page will be so much cleaner.<br /><br /></div>
            
</body>
</html>
Note: this piece of code will hide the answers:
Code:
Code:
<style type="text/css">
	.faq-answer {
	display:none;
	}
</style>
Now the last step is the JavaScript code to make our fancy effects happen:
Code:
Code:
$(document).ready(function() {									
	$("a[name^='faq-']").each(function() {
		$(this).click(function() {
			if( $("#" + this.name).is(':hidden') ) {
				$("#" + this.name).fadeIn('slow');
			} else {
				$("#" + this.name).fadeOut('slow');
			}			
			return false;
		});
	});
});
Here is how it works:
$(document).ready() means after the document is ready and loaded call the function passed to ready() method.
After the document is loaded jQuery will fire our function and the function walks through all the "a" tags
who's name attributes start with "faq-" and attaches an onclick event handler to them.
The event handler when fired, first checks to see if the answer is hidden or visible, in case it's hidden it
displays it with a fadeIn effect: $("#" + this.name).fadeIn('slow'); and returns false and vice versa.
The other important thing to note is that the name attribute of the links are the same as the ID of the DIVs so
we know which DIV is the answer to the clicked link.


So all together we have this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Digg-style FAQ page</title>

<script type="text/javascript" src="jquery.js"> </script>

<script type="text/javascript">
$(document).ready(function() {									
	$("a[name^='faq-']").each(function() {
		$(this).click(function() {
			if( $("#" + this.name).is(':hidden') ) {
				$("#" + this.name).fadeIn('slow');
			} else {
				$("#" + this.name).fadeOut('slow');
			}			
			return false;
		});
	});
});
</script>

<style type="text/css">
.faq-answer {
display:none;
}
</style>

</head>

<body>

   <a href="#" name="faq-1">What is Digg like FAQ?</a><br />
   <div class="faq-answer" id="faq-1">It's like this: http://www.digg.com/faq<br /><br /></div>
   
   <a href="#" name="faq-2">Is it easy to make?</a><br />
   <div class="faq-answer" id="faq-2">Yes it's very easy to make!<br /><br /></div>
   
   <a href="#" name="faq-3">Why would I want to do that?</a><br />
   <div class="faq-answer" id="faq-3">And your page will be so much cleaner.<br /><br /></div>
            
</body>
</html>
It's also cross browser.
Reply With Quote
Sponsor
  #2 (permalink)  
Old 11-13-2008, 08:32 PM
Yankidank's Avatar
Coder/Designer
Pligg Version: SVN
Pligg Template: Wistie
 
Join Date: Dec 2005
Location: Ocala, FL
Posts: 2,916
Send a message via AIM to Yankidank Send a message via Skype™ to Yankidank
Thanks for taking the time to write this guide.

Need a Web Host ?
Save up to $84
when you purchase hosting from Dreamhost.com.
Use the coupon code PLIGG when buying a web hosting package from Dreamhost.com
Reply With Quote
  #3 (permalink)  
Old 11-14-2008, 01:57 AM
magpie2419's Avatar
Casual Pligger
 
Join Date: Jun 2007
Location: Libya
Posts: 67
Yes thanks very much well written very easy to understand and useful
Reply With Quote
  #4 (permalink)  
Old 11-14-2008, 03:10 AM
ArturOzimek's Avatar
Coder
 
Join Date: Jul 2008
Posts: 43
Good job.tnx
Reply With Quote
  #5 (permalink)  
Old 11-14-2008, 06:47 AM
rubber2002's Avatar
Pligg Donor
 
Join Date: Jul 2007
Posts: 309
Has anyone implemented this?
Reply With Quote
  #6 (permalink)  
Old 02-05-2009, 06:46 AM
New Pligger
 
Join Date: Feb 2009
Posts: 1
This is a great help and wonderfull little script. Thank you so much. I have implemented locally and it seems to be working just fine.

I'm not a coder, just a designer. Could you please explain how to make open faq answer close, when clicked on another faq. With this option added, I think it can also be used for other purposes such as making accordion menus etc beside faq pages.
Reply With Quote
  #7 (permalink)  
Old 04-18-2009, 03:07 PM
Casual Pligger
Pligg Version: 9.9.0
Pligg Template: czNet
 
Join Date: Jan 2008
Posts: 63
Hey thanks for the great tutorial the only problem is that it works when i put the code on faq.php but there only appears the faqs does not appear the whole site when i put the code on faq-en_center.tpl it gives me an error, where am i suppose to put the code?

thank you
Reply With Quote
  #8 (permalink)  
Old 05-03-2009, 11:14 AM
Constant Pligger
 
Join Date: Jan 2009
Posts: 201
Anyone try this with the new pligg 1.0? I can't see my answers to the questions when I click on the question. Anyone else having this issues?

Last edited by gjrb29; 05-03-2009 at 10:44 PM.
Reply With Quote
  #9 (permalink)  
Old 05-26-2009, 09:48 AM
New Pligger
 
Join Date: Sep 2008
Posts: 7
very helpful. Thank you.
Reply With Quote
  #10 (permalink)  
Old 06-09-2009, 07:08 PM
Casual Pligger
Pligg Version: 9.9.0
Pligg Template: czNet
 
Join Date: Jan 2008
Posts: 63
Can somebody please answer my question?

thank you
Reply With Quote
Reply

Tags
digg, faq, make, style

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Might be time to think about better use of javascript jbowman General Help 1 07-20-2007 09:19 AM

Pligg Modules and Pligg Templates from Pligg Pro Find support on the Pligg CMS Forum - 24 hours a day! Use the coupon code PLIGG at Dreamhost.com to receive a discount of up to $84.00 Make a donation to support Pligg CMS development