473,406 Members | 2,312 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,406 software developers and data experts.

Q/A Toggle Script

Hi,

First, please forgive my terrible knowledge of JS! I haven't used it in
years.

I am trying to create a Help page where a new question is on each line,
and when clicking on the question, the answer is written below it.
Click on the question again removes the answer. (Basically the Q/A is
being toggled).
I am using the following JS code for each Question (probably not the
best way):

<script language="JavaScript">
function Help1Toggle() {
if(document.help.help1display.value='N') {
document.getElementById('Help1').innerHTML = '<b>A</b> This is the
answer.';
document.help.help1display.value='Y';
}
else if(document.help.help1display.value='Y') {
document.getElementById('Help1').innerHTML = '';
document.help.help1display.value='N';
}
}
</script>
The HTML on the page is:

<form name="help">
<input type="hidden" name="help1display" value="N">
<a href="javascript:;" onClick="Help1Toggle();"><b>Q</b> This is the
Question?</a><br>
<span id="Help1"></span>
</form>
Now, when the page loads, the Question(s) are displayed (and answers
hidden). Clicking on a Question writes the answer below it properly,
and changes the value of the textbox to Y. Problem is, clicking on the
Question again doesn't do anything. It doesn't change the textbox value
to N, and doesn't hide the question. No JS errors are reported. I've
tried this in both Firefox and IE.

Any ideas/easier ways of doing this?

Cheers,

Kingo

May 10 '06 #1
2 3126
Kingo wrote:
Hi,

First, please forgive my terrible knowledge of JS! I haven't used it in
years.

I am trying to create a Help page where a new question is on each line,
and when clicking on the question, the answer is written below it.
Click on the question again removes the answer. (Basically the Q/A is
being toggled).
Then just toggle display of the answer on/off.

I am using the following JS code for each Question (probably not the
best way):

<script language="JavaScript">
The language attribute is deprecated, type is required.

<script type="text/javascript">

function Help1Toggle() {
if(document.help.help1display.value='N') {
This *sets* the value of the input to 'N' and will always return true.
You need the equality equals operator '==', not the assignment operator '='.

When evaluating whether an expression is equivalent to a string, it is
common to put the string on the left so that if an assignment is
accidentally used, an error will result rather than a permanent true,
i.e. use:

if ('N' == document.help.help1display.value)
Now if you accidentally use '=' you will get an error, which hopefully
you'll find straight away rather than after a bit of testing.

document.getElementById('Help1').innerHTML = '<b>A</b> This is the
answer.';
Allowing auto-wrapping of code nearly always introduces errors, manually
wrap at about 70 characters before posting.

There is no need for innerHTML, put the answer in the page and have it
shown by default. If script support is detected, hide the answers and
use a show/hide toggle. It is also nice to have a 'show all' button.

That way if users don't have JS enabled or supported, they can still see
the answers (it is a help page after all).
[...]
<form name="help">
<input type="hidden" name="help1display" value="N">
<a href="javascript:;" onClick="Help1Toggle();"><b>Q</b> This is the
Do not put script in the href attribute. If you don't want an A
element, don't use one. Use a span or div, then style it like a
'clickable' element, e.g.

<... style="cursor:pointer; text-decoration:underline;" ..>

Question?</a><br>
<span id="Help1"></span>
</form>
Now, when the page loads, the Question(s) are displayed (and answers
hidden). Clicking on a Question writes the answer below it properly,
and changes the value of the textbox to Y. Problem is, clicking on the
Question again doesn't do anything. It doesn't change the textbox value
to N, and doesn't hide the question. No JS errors are reported. I've
tried this in both Firefox and IE.
Because your test always returns true, see above.

Any ideas/easier ways of doing this?


Try this thread:

<URL:http://groups.google.co.uk/group/comp.lang.javascript/tree/browse_frm/thread/f120985776684c7a/a177008767f1e129?rnum=1&q=question+answer+hide&_do ne=%2Fgroup%2Fcomp.lang.javascript%2Fbrowse_frm%2F thread%2Ff120985776684c7a%2Fa177008767f1e129%3Fq%3 Dquestion+answer+hide%26rnum%3D1%26#doc_a177008767 f1e129>

--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>
May 10 '06 #2
Thanks, works perfectly.

May 10 '06 #3

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

Similar topics

3
by: Jürgen Heyn | last post by:
Good morning, on form 2 input elements and 1 image are placed. The 1st (Index) one is "hidden", the 2nd ) is an empty textbox. When clicking the picture I would like to toogle a boolean value....
2
by: leegold2 | last post by:
I wondered if anyone would give me code- I think it would be easy, but I'm a complete newbie. What I want to do is to show many tables in a brief truncated format and then for each table offer the...
4
by: Jon | last post by:
I'm looking for a way to toggle text without using client scripting. One way to go is by the visibility property and a click event, but this does a postback and returns to the top of the page. Is...
1
by: barbalu | last post by:
Hello. I'm painfully new to this and am having trouble figuring out what I'm doing wrong... I have two small tables in a simple html file. I want each table to have the ability to collapse/expand...
1
by: swiftouch | last post by:
I'm getting an error message in FF2.0: document.getElementById(toggle) has no properties The goal of the script is, when I hover my mouse over an image, to make one div element visible while...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I need to toggle the text on an html input button in a .net web application. the name of the button is btn_hide and I do have an onclick function for it. <script language="javascript"...
4
by: ameshkin | last post by:
I have a checkbox with an ID of svc_tp_1, and an image that corresponds with this checkbox below it. <input type="checkbox" name="checkbox" id="svc_tp_1" value="svc_tp_1" / <img...
1
by: student4lifer | last post by:
Hello, I currently have a hyperlink to toggle the node, showing and hiding the list. Could someone show me how to use a checkbox instead of the hyperlink to do the same thing? Thanks. I...
1
by: Amit1980 | last post by:
I have to toggle the arrow image on onclick event. Here is the code with the required functionility. What I want now is, when the page loads there will be right arrow images. The arrow should be...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.