Solved

Javascript needed to add multiple selections to a text area

Posted on 2011-09-28
2
172 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
An analysis of the phishing scam that has been affecting Google users, along with steps to take for protection, as well as what to do if you receive one of the emails.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now