Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple select element - onchange

I have a select element that allows for multiple selections. I'd like to display the selected values in another part of the page (in a div or something) as the user makes changes to what is selected.

Is the only way of doing this to iterate over the "options" and check if "selected" is true? this would not be preferable since each "onchange" event would require the entire select element to be iterated over.

Here's a fiddle that demonstrates how I am currently doing it, but I'm hoping maybe there's a better way than having to iterate over all the options on every "change": multiple select elment onchange fiddle

like image 949
codecraig Avatar asked Apr 05 '11 12:04

codecraig


People also ask

How do you attach a single Onchange event with multiple dropdowns?

Best procedure for cases like this would be, to add a common class to all the dropdowns for which you want to call the function on change. For ex: add 'trigger-change' class for all your required dropdowns. Then below bind event should work perfect for you. $('form select.

Can I use Onchange in select tag?

Event Handling With the Select ElementThe onChange attribute of <select> lets you dynamically change something on screen, without reloading the page. For example, your can use it to display a particular image, depending on the user's selection.


5 Answers

.val() on a multiple select returns an array.

See the snippet below as an example:

$(function() {
    $('#fruits').change(function(e) {
        var selected = $(e.target).val();
        console.dir(selected);
    }); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select multiple="true" id="fruits">
    <option value="apple">Apple</option>
    <option value="banana">Banana</option>
    <option value="mango">Mango</option>
    <option value="grape">Grape</option>
    <option value="watermelon">watermelon</option>
</select>
like image 194
StuperUser Avatar answered Sep 25 '22 05:09

StuperUser


In your fiddle, I just used .val(). This returns an array

JSFiddle Link

$(function() {
    $('#fruits').change(function() {
        console.log($(this).val());
    }); 
});
like image 32
JohnP Avatar answered Sep 24 '22 05:09

JohnP


If you could use jQuery it might be as easy as:

$('select').change(function() {alert($(this).val())})
like image 32
zindel Avatar answered Sep 28 '22 05:09

zindel


You could use blur instead of change, so that the select is only processed once, rather than on each selection. http://jsfiddle.net/2mSUS/3/

like image 20
shanethehat Avatar answered Sep 25 '22 05:09

shanethehat


$(function() {
    $('#fruits').change(function(e) {
        var selected = $(e.target).val();
        console.dir(selected);
    }); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select multiple="true" id="fruits">
    <option value="apple">Apple</option>
    <option value="banana">Banana</option>
    <option value="mango">Mango</option>
    <option value="grape">Grape</option>
    <option value="watermelon">watermelon</option>
</select>
like image 22
Anand Avatar answered Sep 25 '22 05:09

Anand