...

View Full Version : Having a checkbox send information to a textarea



Rogier
07-04-2006, 05:42 PM
Hi all,


I am setting up a form which will ask the user for several questions after which they will then be saved to a database. It all works well with exception of one thing. I am trying the following and can't get it to work.

What I want to do is to to have multiple checkboxes, each of them activating the same textarea and then add information to the textarea.

For activating the textarea I am using the following code:


<script>
function SetState(obj_checkbox, obj_textarea){
if(obj_checkbox.checked){obj_textarea.disabled = false;}
else{ obj_textarea.disabled = true;}}
</script>



And the checkbox has the following attribute:


onclick="SetState(this,this.form.NAMEOFTEXTAREA)"

This works, however, how do I add information to the textarea when each of the checkboxes is checked? I tried obj_textarea.value='VALUE'; but this will only allow one checkbox to display its value into the textarea.



Example:


There are three checkboxes:

Checkbox 1
Checkbox 2
Checkbox 3

When checkbox 1 is checked, the textarea should be activated and read "Info 1"
When checkbox 2 is checked, information should be added to the textarea, thus reading: "Info 1, Info 2"
When checkbox 3 is checked, information should be added to the textarea, thus reading: "Info 1, Info 2, Info 3"

Now, when one of the boxes is unchecked, it would be nice if the info 1, 2 or 3 would be removed again (this is not a necessity though). If one box is checked randomly, for example only checkbox 2, the textarea should still be activated and read "Info 2".

Do you think this is possible?

Thanks in advance!


Rogier

Lerura
07-05-2006, 12:37 AM
you don't have to enable/disable the textarea.
you should refresh the textarea when one of the checkboxes changes state

Give all the checkboxes a common name, and let them run the same script without parameters


<input type="checkbox" name="disabler" onclick="RefreshTextArea()">
<input type="checkbox" name="disabler" onclick="RefreshTextArea()">
<input type="checkbox" name="disabler" onclick="RefreshTextArea()">

Give your textarea an id:

<textarea id="ShowInfos"></textarea>
and store the informations in an array:

Infos=new Array();
Infos[0]="Info 1";
Infos[1]="Info 2";
Infos[2]="Info 3";

your script could then look like this:


<script type="text/javascript"><!--
Infos=new Array();
Infos[0]="Info 1";
Infos[1]="Info 2";
Infos[2]="Info 3";

function StoreButtons(){
Buttons=document.getElementsByName('disabler'); // the script will remember the checkboxes as Buttons[0], Buttons[1] and Buttons[2]
}
window.onload=StoreButtons;

function RefreshTextArea(){
UsedInfos=new Array; // Creates/Resets an temporary array.
for (x=0;x<Buttons.length;x++){
if (Buttons[x].checked){
UsedInfos[UsedInfos.length]=Infos[x]; // copies the relevant Infos to the temporary array;
}
}
Text=UsedInfos.join('\n\n'); // creates a string with 2 linefeeds between the used Infos.
document.getElementById('ShowInfos').value=Text;
}
// --></script>

Rogier
07-05-2006, 09:39 AM
Yes, that did the trick! Wow, I didn't think this would be possible. Thanks a million lerura!

Greetings from Holland,



Rogier



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum