Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Having a checkbox send information to a textarea

    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:

    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:

    Code:
    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

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    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

    Code:
    <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:
    Code:
    <textarea id="ShowInfos"></textarea>
    and store the informations in an array:
    Code:
    Infos=new Array();
    Infos[0]="Info 1";
    Infos[1]="Info 2";
    Infos[2]="Info 3";
    your script could then look like this:

    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, that did the trick! Wow, I didn't think this would be possible. Thanks a million lerura!

    Greetings from Holland,



    Rogier


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •