-1

I am having trouble to store all the selected options as a list, my code only sotres the first selected option and ignores the rest. How can I solve this problems?

<label for="itemlist">Items</label>
<br>
<select id="itemlist" name="itemlist" multiple="multiple" required>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

<script type="text/javascript">                                 
    $(function() {
        $('#itemlist').change(function() {
            console.log($(this).val());
        }).multipleSelect({
            width: '100%'
        });
    });

</script>
user1993
  • 1
  • 3
  • 1
    Are you facing any issue/error? - Because `console.log` shows array of selected values. – Arindam Nayak Oct 13 '14 at 18:23
  • Solve what problem? You only said you're having trouble. What kind of trouble are you having? – Alex Wayne Oct 13 '14 at 18:37
  • For one thing, your Id is itesmlist, and you're selector is looking for #itemlist. – Scottie Oct 13 '14 at 19:54
  • What I’m trying to do is store the selected values into my database as a list, separated by commas. The code from above only stores the first selected value into my database and ignores the rest. Any suggestions? – user1993 Oct 13 '14 at 21:17

1 Answers1

1

Gus,

As you are using it jQuery already $('selector').val(), this will retreive (comma separated) all values selected on the select tag.

Here is an example I've wrote for you using the .on('click', function).

FIDDLE

Hope this helps!

  • I tried it but for some reason it still only stores one selected value into my database and not the list of values separated by commas like I plan to. – user1993 Oct 14 '14 at 14:55
  • maybe the problem is not in the javascript is in the request going to your db, or how you process that. Please give us more background, for example how re you storing that information? – Joaquin Oscar Ibar Oct 15 '14 at 02:33