Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript needed to add multiple selections to a text area

Posted on 2011-09-28
2
Medium Priority
?
191 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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