473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Showing hidden answer when question clicked?

I've Googled, but can't find what I need, perhaps I asking the wrong
question!

I want a "FAQ" page on a web site, I hate those pages that scroll you to
the answer so and I figured that a good way to do it would be to have
hidden content under each question, something like this [the text in the
brackets should appear when the question is clicked]:

What is the first letter of the alphabet?
[The first letter of the alphabet is "A"]

What is 5 + 5?
[5 + 5 is 10]

I'd like to have each answer loaded, so it appears straight away, and
ideally in a form that I can easily edit.

Any suggestions?
--
Nigel M

"Time may be a great healer,
but he's a lousy beautician"
Jul 23 '05 #1
4 3010
Nigel Molesworth wrote:
I've Googled, but can't find what I need, perhaps I asking the wrong
question!

I want a "FAQ" page on a web site, I hate those pages that scroll you to
the answer so and I figured that a good way to do it would be to have
hidden content under each question, something like this [the text in the
brackets should appear when the question is clicked]:

What is the first letter of the alphabet?
[The first letter of the alphabet is "A"]

What is 5 + 5?
[5 + 5 is 10]

I'd like to have each answer loaded, so it appears straight away, and
ideally in a form that I can easily edit.

Any suggestions?


The following shows/hides answers when the question is clicked. It
should be pretty easy to maintain - the answers are only hidden and the
hide/show onclick function is only added if scripting support is
available. Put the styles and script in external files and you have a
very simple page where non-script visitors are not too badly treated.

Each question id needs to have a matching answer id.

The script depends on getElementById and getElementsByTagName, you
should read the group FAQ regarding support for browsers that are
dependent on document.all (e.g. IE 4) if you wish to support them.

<URL:http://www.jibbering.com/faq/#FAQ4_15>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Q & A </title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">

<style type="text/css">
..Q, .A {
font-family: verdana, sans-serif;
width: 20em;
}
..Q {
cursor: pointer;
font-weight: bold;
padding: 15px 5px 5px 5px;
color: #333366;
background-color: #CCFFFF;
}
..A {
padding: 5px 5px 15px 5px;
color: #CC0033;
background-color: #FFFF99;
border: 1px solid #6600FF;
}
</style>
<script type="text/javascript">
function initQnA() {
if ( !document.getElementById || !document.getElementsByTagName ) {
return null;
}
var divs = document.getElementsByTagName('div')
var x, i = divs.length;
if ( divs[0].style ) {
while ( i-- ) {
x = divs[i];
if ( x.className ) {
if ( 'Q' == x.className ) {
x.onclick = function() {showHideA(this)};
x.title = 'Click to show/hide answer';
} else if ( 'A' == x.className ) {
x.style.display = 'none';
}
}
}
}
}

function showHideA(dQ){
var dAid = 'A-' + dQ.id.split('-')[1];
var dA = document.getElementById(dAid);
dA.style.display = ( 'none' == dA.style.display )? '' : 'none';
}

window.onload = initQnA;

</script>
</head><body>

<div id="Q-1" class="Q">What is the first letter of the alphabet?</div>
<div id="A-1" class="A">[The first letter of the alphabet is "A"]</div>

<div id="Q-2" class="Q">What is 5 + 5?</div>
<div id="A-2" class="A">[5 + 5 is 10]</div>

</body>
</html>

--
Rob
Jul 23 '05 #2
In comp.lang.javascript, RobG wrote:
The following shows/hides answers when the question is clicked.


Absolutely fantastic, exactly what I needed. Thank you so much.
--
Nigel M

"Time may be a great healer,
but he's a lousy beautician"
Jul 23 '05 #3
JRS: In article <8p********************************@4ax.com>, dated
Mon, 13 Jun 2005 23:39:49, seen in news:comp.lang.javascript, Nigel
Molesworth <re***@thegroup.com> posted :

I want a "FAQ" page on a web site, I hate those pages that scroll you to
the answer so and I figured that a good way to do it would be to have
hidden content under each question, something like this [the text in the
brackets should appear when the question is clicked]:


You might prefer that, but will all your possible readers prefer it?

I suggest adding a "Reveal All" button at the top, which on being
clicked changes its label and function to Hide All.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
ASM
RobG wrote:
Nigel Molesworth wrote:
I've Googled, but can't find what I need, perhaps I asking the wrong
question!

I want a "FAQ" page on a web site, I hate those pages that scroll you to
the answer so and I figured that a good way to do it would be to have
hidden content under each question, something like this [the text in the
brackets should appear when the question is clicked]:

What is the first letter of the alphabet?
[The first letter of the alphabet is "A"]

What is 5 + 5?
[5 + 5 is 10]

I'd like to have each answer loaded, so it appears straight away, and
ideally in a form that I can easily edit.

Any suggestions?


The following shows/hides answers when the question is clicked.

and following would work on my NC4 :-)

<form>
<p>What is 5 + 5?
<input type=button onclick="R_001.value=' 5 + 5 is 10';" value="?">
<input type=text name="R_001">
<p>What is 5 x 5?
<input type=button onclick="R_002.value=' 5 x 5 is 25';" value="?">
<input type=text name="R_002">
</form>
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Nigel Molesworth | last post by:
I've Googled, but can't find what I need, perhaps I asking the wrong question! I want a "FAQ" page on a web site, I hate those pages that scroll you to the answer so and I figured that a good...
2
by: c.anandkumar | last post by:
Hi All - I have some problems getting a small piece of javascript working correctly for Firefox. Here is what I am trying to do - 1. I have a form (like a search form) 2. I have many groups...
5
by: gregmercer | last post by:
I have the following html sample, where I'd like to have a show and hide two divs, one replacing the other. Following these two divs is a third div (the bluediv) which I would like to have placed...
2
by: Kevin | last post by:
I've got a problem where I need to know the value of a hidden field inside a repeater once a button is clicked. Ths code inside my repeater looks like this: <input id="conf_num" type="hidden"...
3
by: Tom | last post by:
Sorry... new to this. I trying to figure out how to do a page redirect when someone clicks a treeview node. Does someone have a code snippet for this?
4
by: Hans Merkl | last post by:
Hi, Is there a way to show the column headers of a GridView control even if there is no data? The only thing I see is the EmptyDataTemplate but I would also like to display the column headers. ...
5
by: graphicsxp | last post by:
Hi, I have can upload a file to the server from my aspx page using the uploadfile control. When the user click on submit I'd like to show an animated gif while the file is being uploaded. But how...
6
by: =?Utf-8?B?L2Rldi9udWxs?= | last post by:
Hello, i am using visual studio 2003 enterprise architect version. I am making apps for the .Net framework 1.1. While testing an interface, i discovered something strange. In this application...
5
by: anonymousstar | last post by:
Hi all, I am new to these forums and decided to join one (this one) after 5 days of researching to try and solve my problem. I am creating a booking type of calendar. It will be run by an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.