473,657 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript">
function Help1Toggle() {
if(document.hel p.help1display. value='N') {
document.getEle mentById('Help1 ').innerHTML = '<b>A</b> This is the
answer.';
document.help.h elp1display.val ue='Y';
}
else if(document.hel p.help1display. value='Y') {
document.getEle mentById('Help1 ').innerHTML = '';
document.help.h elp1display.val ue='N';
}
}
</script>
The HTML on the page is:

<form name="help">
<input type="hidden" name="help1disp lay" value="N">
<a href="javascrip t:;" onClick="Help1T oggle();"><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 3143
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="JavaS cript">
The language attribute is deprecated, type is required.

<script type="text/javascript">

function Help1Toggle() {
if(document.hel p.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.h elp1display.val ue)
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.getEle mentById('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="help1disp lay" value="N">
<a href="javascrip t:;" onClick="Help1T oggle();"><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:p ointer; text-decoration:unde rline;" ..>

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.c o.uk/group/comp.lang.javas cript/tree/browse_frm/thread/f120985776684c7 a/a177008767f1e12 9?rnum=1&q=ques tion+answer+hid e&_done=%2Fgrou p%2Fcomp.lang.j avascript%2Fbro wse_frm%2Fthrea d%2Ff1209857766 84c7a%2Fa177008 767f1e129%3Fq%3 Dquestion+answe r+hide%26rnum%3 D1%26#doc_a1770 08767f1e129>

--
Rob
Group FAQ: <URL:http://www.jibbering.c om/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
2373
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. (displayed in the textbox). The following function reacts correctly on the first click. Any further click as no effect (is returns "TRUE" in the textbox). Where is the mistake ? How can I solve the problem ? Thank you very much in advance.
2
2285
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 user the ability to toggle so they can see the full content of each table individually. The code I got from the net below works fine for *one* table but when I try to add a 2nd table it does not work. How can I apply this toggle individually to...
4
1655
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 there a way around this? Jon Cosby
1
5231
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 when the user clicks selected text. I've attempted the javascript toggle function. I believe the problem is somewhere in how I'm calling the function, but I dont know exact what... Any help would be much appreciated! Thanks! <html> <head>...
1
1956
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 making all the others div elements in the array, hidden. thisDocId is the element to avoid hiding. This is the script: <script>
3
1559
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" type="text/javascript"> // <!CDATA]> </script> -- thanks, Paul G Software engineer.
4
6155
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 src="images/screen_print.jpg" onclick="toggle('svc_tp_1'); return true;" /> I know how to get the checkbox to check when the image is clicked, but what I really want is a toggle. When a user clicks on the image, the system should first check to see if svc_tp_1...
1
2986
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 tried the following line but the node's nextSibling becomes instead of <div>. Then I tried node. nextSibling.nextSibling but it didn't work. Please help!
1
5909
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 chenge to down arrow when I the row expands to dispaly the child links. and if the collaps to hide child link the arrow should change back to right arroe image. In the below e.g code you will not be able to see the arrow images. Here is the code I have....
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.