SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with select box and javascript

    Hi,

    I am working on a project where I need the contents of a select box to be added to a textarea when a selection is clicked.

    For example, say I have a list box with 10 elements that allows multiple selections by holding down CTRL and clicking whatever you want. I also have a textarea. How can I make the text of the selected elements of the select box appear in the textarea as they are selected?

    Any help is appreciated.

    Thanks,
    Martin

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <form>
    <select name="mySelect" multiple size="4" onchange="doThis(this)">
    <option value="0"></option>
    <option value="1">Apple</option>
    <option value="2">Blueberry</option>
    <option value="3">Cherry</option>
    <option value="4">Durian</option>
    <option value="5">Fig</option>
    <option value="6">Guanabana</option>
    <option value="7">Huckleberry</option>
    <option value="8">Ilang Ilang</option>
    <option value="9">Jackfruit</option>
    </select><br><br>
    <textarea name="myTextarea" cols="25" rows="6"></textarea>
    </form>
    <script>
    function doThis(s){
    var x = s.options;
    var taValue = '';
    for (i=0;i<x.length;o=i++){
    if (x[i].selected)
    taValue += x[i].text+'\n';
    }
    document.forms[0].myTextarea.value = taValue;
    }
    </script>

  3. #3
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply....thats sort of what i am looking for. However, there are 2 other things I probably should have mentioned in my post. I need the current contents of the textarea to stay there and i need the values from the select box to be added in the order they are selected.

    Thanks,
    Martin

  4. #4
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (edit): Looks like shoop already has the += ....

    Use +=

    For example, you already have the code to get the select value:

    Code:
    var tArea = document.getElementById('textboxName')
    tArea.value += otherStuff;
    otherStuff would be the code grabbed from the select box. It would then append it to the end of whatever text is inside the textrea by use of the +=
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •