Turo
Turo

Reputation: 1607

Multiselect in jQuery with shift key

I would like to allow multiple selection of items so that the #par displays all 3 values selected with a shift. This post leads me to believe such functionality is not built in with jQuery, and needs a workaround, although it is from 3 years ago. I use jsFiddle and 1.9.1 jQuery.

<select id="select" multiple>
    <option value="item1">item1</option>
    <option value="item2">item2</option>
    <option value="item3">item3</option>
</select>

<br />
<p id="par"></p>
$('#select').change(function(){
    $('#select option:selected').each(function(){
        $("#par").text($(this).text() + ", ");
    });
});

Upvotes: 0

Views: 389

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337590

You can get a comma separated list of the values chosen in a multiple select by using val() - the same as you would any other form element. Try this:

$('#select').change(function() {
    $("#par").text($(this).val());
});

Example fiddle

Upvotes: 2

Related Questions