Solved

Javascript needed to add multiple selections to a text area

Posted on 2011-09-28
2
174 Views
Last Modified: 2012-05-12
I have a drop-down box with a list of items, user selects from that list, clicks on a Submit button
and the item selected is appended to a text area. The following code works OK for a single list item,
but I'd like to allow the user to select multiple items (ctrl-click or shift-click) to add multiple items.
How can I modify my javascript code to do this?


      function add_users() {
        if (document.getElementById('to_text').value.length > 0)
          document.getElementById('to_text').value += ',';
        document.getElementById('to_text').value += document.getElementById("to_select").options[document.getElementById("to_select").selectedIndex].value;
      };


              <select id="to_select" size="5" multiple="multiple" name="add_tos[]">
              <?    // php code here to fetch items from database and loop
                        echo '<option>' . $row['firstname'] . ' ' . $row['lastname'] . ' &lt' . $row['email'] . '&gt</option>';
                ?>
              </select>
              <input id="add_button" type="button" value="Add" onClick="add_users();"/>
0
Comment
Question by:jimdgar2
2 Comments
 
LVL 2

Accepted Solution

by:
jmgst116 earned 500 total points
ID: 36791285
function add_users() {
          for(var ii=0;ii<document.getElementById("to_select").options.length;ii++)
                  if(document.getElementById("to_select").options[ii].selected)
                  {
                       if (document.getElementById('to_text').value.length > 0)
                          document.getElementById('to_text').value += ',';
                       document.getElementById('to_text').value += document.getElementById("to_select").options[ii].value;
                  }
      };
0
 

Author Closing Comment

by:jimdgar2
ID: 36812644
Bingo! Thank you.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Batch File to Telnet 10 3,414
Help with modifying the navigation menu of a website 3 240
Sort my list 5 388
script for sleep and wakeup on PC 2 120
This tutorial shows how to create a greeting card by combining two image layers and a text layer on a PC using a free image editing app.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question