Select2 if first option selected disable the rest - jquery-select2

I have this select2 where
If first option is selected, all other options should be disabled.
If any other option is selected, first option should be disabled.
Any help?
<select class="select2">
<option value="Package">Complete Package</option>
<option value="Hotel">Hotel Only</option>
<option value="Transport">Transport Only</option>
<option value="Misc">Misc</option>
</select>

Library select2
https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js
https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css
**reference fiddle** : https://jsfiddle.net/bindrid/hpkqxto6/
<select multiple id="example" style="width: 300px">
<option value="Package">Complete Package</option>
<option value="Hotel">Hotel Only</option>
<option value="Transport">Transport Only</option>
<option value="Misc">Misc</option>
</select>
$(function() {
$('#example').select2();
//Select2 Event handler for selecting an item
$('#example').on("select2:selecting", function(evt, f, g) {
disableSel2(evt, this, true);
});
// Select2 Event handler for unselecting an item
$('#example').on("select2:unselecting", function(evt) {
disableSel2(evt, this, false);
});
});
// At some point during the select2 instantation it created the
// data object it needs with the source select option.
// This function, called by the events above to set the current status for the
// group for which the selected option belongs.
function disableSel2(evt, target, disabled) {
// Found a note in the Select2 formums on how to get the item to be selected
var selId = evt.params.args.data.id;
var aaList = $("option", target);
$.each(aaList, function(idx, item) {
var data = $(item).data("data");
if (selId == "Package") {
if(data.id != selId){
data.disabled = disabled;
$('#example > option').prop("selected", false);
}
}
})
}

Related

How to set an id to option in HTML.DropDownList

I have an HTML.DropDownList where I can set datavaluefield and datatextfield like the following,
<select>
<option value="Fruit">APPLE</option>
</select>
Even, I can able to set the ID to dropdown list using html attributes but Since I need to set the 'Id' to the option like following,
<select>
<option ID='1' value="Fruit">APPLE</option>
</select>
Can anyone share the idea on it...
You can use the id attribute to set the ID to the option tag and use this.value into the onchange callback with select element.
Below is the working example
document.getElementById("select1").onchange = function() {
if (this.value == 'Fruit') {
var optionID=document.getElementById('1');
alert(optionID.value);
}
if (this.value == 'Flower') {
var optionID=document.getElementById('2');
alert(optionID.value);
}
}
<select name="select01" id="select1" onchange="handleSelect()">
<option value="Fruit" id="1">Apple</option>
<option value="Flower" id="2">Rose</option>
</select>
Try this, It will alert your selected value
<select onchange="alert(this.value)">
<option id="1">APPLE</option>
<option id="2">Orange</option>
</select>

Already selected options which are disabled should not give to remove in select 2

Is there an option to block 'remove option' (or hide remove button) from tags which are already selected and disabled.
Here is my code in jsfiddle.
https://jsfiddle.net/wphqwvLf/734/
<select id="test" multiple>
<option val="1" selected disabled>1111</option>
<option val="2">222</option>
<option val="3" >333</option>
<option val="4" disabled>444</option>
</select>
$(function () {
$('select')
.select2( {
tags: true
}
)
.on('change', function(){
//console.log( $(this).val() );
})
.on('select2:unselecting',function(){
console.log('removes' );
});
});
In that code, I need to remove 'remove button' in 1111 tag OR disable 1111 from removing.

Select2 trigger select all checkbox

I am using Select2 dropdown, my requirement is to check uncheck checkbox value. If all options are selected then checkbox checked and if all options are not selected then checkbox uncheck.
HTML
<select multiple id="e1" style="width:300px">
<option value="AL">Alabama</option>
<option value="Am">Amalapuram</option>
<option value="An">Anakapalli</option>
<option value="Ak">Akkayapalem</option>
<option value="WY">Wyoming</option>
</select>
<input type="checkbox" id="checkbox" >Select All
<input type="button" id="button" value="check Selected">
JS
$("#e1").select2();
$("#checkbox").click(function(){
if($("#checkbox").is(':checked') ){
$("#e1 > option").prop("selected","selected");
$("#e1").trigger("change");
}else{
$("#e1 > option").removeAttr("selected");
$("#e1").trigger("change");
}
});
$("#button").click(function(){
alert($("#e1").val());
});
Fiddle
http://jsfiddle.net/jEADR/62/.
You need to catch the change event, when event fire you need check how many options chosen and compare them with all of the options:
$("#e1").on("change", function(e) {
var count = this.selectedOptions.length // how options selected
var opt = this.length; // how options total
if (count===opt) {
// all selected
$("#checkbox").prop('checked', true);
} else {
// not all selected
$("#checkbox").prop('checked', false);
}
});
http://jsfiddle.net/jEADR/1834/

Autocomplete Change Event firing after button click

I am using the JQuery UI Autocomplete. I needed it to MustMatch so I set it up like this:
$('#MyAutoComplete').autocomplete({
source: result,
minLength: 1,
change: function (event, ui) {
if (!ui.item) {
$('#MyAutoComplete').val('');
}
}
});
Don't worry about the result as the field is populated and is working as expected. When someone types in a value not in the list and clicks off the field, the field gets erased as I wished--except when I click a submit button. A simplified version of my submit handler is:
$('#MyButton').get(0).onclick = $('#MyForm').get(0).onsubmit = (function () {
$('#MyForm').validate();
var isValid = $('#MyForm').valid();
if (!isValid) {
return false;
}
});
If I click directly from the focused AutoComplete with bad data to the submit button, the submit event happens before the change for the AutoComplete. So the bad data gets submitted. Any other action clears the bad data which causes the submit to fail (because the field is required).
Any ideas how to get around this?
I have tried adding each of these to the top of the submit event. None made a difference.
$('#MyAutoComeplete').data('autocomplete')._trigger('change');
$('#MyAutoComplete').blur();
$('#MyButton').focus();
Does the combobox feature require a higher version of the script then I am using? If I take this:
$('#MyAutoComplete').autocomplete({
source: result,
minLength: 1,
change: function (event, ui) {
if (!ui.item) {
$('#MyAutoComplete').val('');
}
}
});
And turn it into this:
$('#MyAutoComplete').combobox({
source: result,
minLength: 1,
change: function (event, ui) {
if (!ui.item) {
$('#MyAutoComplete').val('');
}
}
});
I get this error:
JavaScript runtime error: Object doesn't support property or method 'combobox'
I am using:
jQuery UI 1.8.7
If it must be in the list, you should use the combobox functionality. The basic syntax is:
<select id="combobox">
<option value="">Select one...</option>
<option value="ActionScript">ActionScript</option>
<option value="AppleScript">AppleScript</option>
<option value="Asp">Asp</option>
<option value="BASIC">BASIC</option>
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Clojure">Clojure</option>
<option value="COBOL">COBOL</option>
<option value="ColdFusion">ColdFusion</option>
<option value="Erlang">Erlang</option>
<option value="Fortran">Fortran</option>
<option value="Groovy">Groovy</option>
<option value="Haskell">Haskell</option>
<option value="Java">Java</option>
<option value="JavaScript">JavaScript</option>
<option value="Lisp">Lisp</option>
<option value="Perl">Perl</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Ruby">Ruby</option>
<option value="Scala">Scala</option>
</select>
<script>
$(function() {
$( "#combobox" ).combobox();
});
</script>
You can see more on how to use it at http://jqueryui.com/autocomplete/#combobox

How do I enable/disable options in Select Box using jquery

I have a select box which contains the options and optgroup that are generated dynamically using php.Now when I select "ALL" all the other optgroup, options should be disabled and when I select any option other than "ALL" the "ALL" option should be disabled
<select name="select1" id ="select1" onchange="handleSelect()">
<option value ="-1">ALL</option>
<optgroup label="CARS">
<option value="ford">FORD</option>
<option value="Nissan">Nissan</option>
</optgroup>
</select>
<script>
function handleSelect() {
var selectVal = $("#select1:selected").text();
if (selectVal == "ALL") {
// cannot disable all the options in select box
$("#select1 option").attr("disabled", "disabled");
}
else {
$("#select1 option[value='-1']").attr('disabled', 'disabled');
$("#select1 option").attr('disabled', '');
}
}
</script>
How can I make this working?
This is kind of a strange thing to be doing but here's code that meets your requirements.
$('select').on('change', function() {
if (this.value == '-1') {
$('optgroup option').prop('disabled', true);
} else {
$('optgroup option').prop('disabled', false);
}
});
Live Example - http://jsfiddle.net/NpNFh/
You can use the following code.Let us consider you have three select boxes as follows
<select class="sel_box" id="sel_1" onchange="disable_sel(this.value);" ></select>
<select class="sel_box" id="sel_2" onchange="disable_sel(this.value);" ></select>
<select class="sel_box" id="sel_3" onchange="disable_sel(this.value);" ></select>
and in function let 'opt' is argument now use following
$('.sel_box option[value="'+opt+'"]').attr("disabled", true);
You can use two variations on the syntax for the disabled attribute, depending on the version of HTML you are targeting:
HTML4: <option value="spider" disabled>Spider</option>
XHTML: <option value="spider" disabled="disabled">Spider</option>

Resources