I want user to select maximum of only three options from multiple select options. I tried this code so far:
You are using this inside change event, which is reference to select.
Try this:
$("select").on('click', 'option', function() {
if ($("select option:selected").length > 3) {
$(this).removeAttr("selected");
// alert('You can select upto 3 options only');
}
});
Demo: http://jsfiddle.net/tusharj/tmkzebnj/
EDIT
$("select").on('change', function(e) {
if (Object.keys($(this).val()).length > 3) {
$('option[value="' + $(this).val().toString().split(',')[3] + '"]').prop('selected', false);
}
});
Demo: http://jsfiddle.net/tusharj/tmkzebnj/1/