jQuery :selected on multiple select selects only last option selected -


i trying add class selected options on multiple select element. code working new class being added, however, on submit, getting value last option selected.

basically making normal select multiple select attribute. multiple select working if remove entire code adds new class.

what doing wrong?

var selects = $('#pwpusform select[multiple="multiple"]');    $(selects).each(function() {      $(this).change(function() {      var selected = $(this).find('option').filter(':selected');      $(selected).each(function() {        $(this).addclass('optionselected');      });    }).change();    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  html, getting value of option 5 selected @ last.  <br/>  <select name="scattrmselect[]" class="pwpus-select-input" multiple="multiple">    <option value="10" class="optionselected">select 1</option>    <option value="20">select 2</option>    <option value="30" selected="selected" class="optionselected">select 3</option>    <option value="40">select 4</option>    <option value="50" class="optionselected">select 5</option>  </select>

how this:

$(".pwpus-select-input").change(function(){        var options = $(this).find('option').filter(':selected');        $(options).each(function(i,item){           $(item).removeclass("optionselected").addclass("optionselected");        });  }); 

working fiddle: https://jsfiddle.net/8b6clv44/


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -