3

Is it possible to prevent an option from being deselected in a select multiple control? Also it must not deselect any other selected options. I have tried:

$('#selections option').click(function(){ $(this).prop('selected', true); }); 

Doesn't seem to work. Anyone know how I can achieve this?

1
  • options don't generally trigger mouse events, so I would say no ? Commented Feb 5, 2014 at 21:32

3 Answers 3

1

Here's one way to do it

$('#selections').on('change', function(e) { var self = this, selected = $(this).data('selected') || []; $.each(selected, function(_,i) { $('option', self).eq(i).prop('selected', true) }); $(this).data('selected', $.map($('option:selected', this), function(el) { return $(el).index(); })); }); 

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

0

already tried this?:

$('#selections option').click(function(evt){ evt.preventDefault(); $(this).prop('selected', true); }); 

1 Comment

If you're proposing this solution then suggest it rather than questioning it. And also explain why this works by editing the answer.
0

Try this:

$("#selections").click(function () { $("#option_always_selected").prop('selected', true); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.