Solved

Javascript needed to add multiple selections to a text area

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A Short Story about the Best File Recovery Software – Acronis True Image 2017
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

25 Experts available now in Live!

Get 1:1 Help Now