Solved

Javascript needed to add multiple selections to a text area

Posted on 2011-09-28
2
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
Let's recap what we learned from yesterday's Skyport Systems webinar.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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