Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  32] [ 2]  / answers: 1 / hits: 17548  / 7 Years ago, fri, june 23, 2017, 12:00:00

I want to deselect the selected option in multiple select based on the selected option's value



I have already tried



jQuery remove selected option from this
and



jQuery remove options from select



<select size=5 id=skills  multiple>
<option value=0>Choose - Skills</option>
<option value=19>Analytics</option>
<option value=20>Android</option>
</select>

<input type=button id=addskills value=Add>


<div class=skill-text>
</div>

<script>
$(function(){
$('#addskills').on('click', function(){
$('#skills :selected').each(function(i, selected){
var text = $(this).text();
var value = $(this).val();
$('.skill-text').prepend(<span class='removeskill' data-id='+value+'>+text+<i class='fa fa-times' ></i></span>);
});
});

$('body').on('click',.removeskill, function(){
var id = $(this).data(id);
//$('body').find(#skills option[value=+id+]).removeAttr(selected);
//$('#skills :selected').find(option[value=+id+]).removeAttr(selected);
$('#skills :selected').each(function(i, selected){
if($(this).val() == id){
alert('inside if');
$(this).removeAttr('selected');
}
});
});
})
</script>


please see the fiddle https://jsfiddle.net/sxf6v3mt/2/


More From » jquery

 Answers
2

You can use $(select option[value='+id+']).prop(selected, false);



It will deselect option which has value set as id



$('body').on('click',.removeskill, function(){
var id = $(this).data(id);
$(this).remove(); // to remove that item from list
$('#skills :selected').each(function(i, selected){
if($(this).val() == id){
$(select option[value='+id+']).prop(selected, false); // to deselect that option from selectbox
}
});
});


Try: https://jsfiddle.net/p1Lu83a2/


[#57332] Wednesday, June 21, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
Mon, Feb 24, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;