I attempt to disable my specific bootstrap select option using javascript.
I know how to disable \"normal select option\", but when using bootstrap select it just di
As described here, you need to re-render the select picker after changing the disabled property of an option.
This should do the trick: (JSFiddle)
function disableDropdown(){
var selectobject;
selectobject = document.getElementById("dropdownBranch").getElementsByTagName("option");
selectobject[3].disabled = true;
$('#dropdownBranch').selectpicker('render');
}
use jQuery
$("#dropdownBranch").attr("disabled", "disabled");
or the pure javascript one:
function disableDropdown(){
document.getElementById("dropdownBranch").setAttribute("disabled", "disabled");
}
JSFiddle
Using render
as mentioned above is no longer the working. As written in their Docs refresh
is the method to use after changing options.
To programmatically update a select with JavaScript, first manipulate the select, then use the refresh method to update the UI to match the new state. This is necessary when removing or adding options, or when disabling/enabling a select via JavaScript.
function disableDropdown(){
var selectobject;
selectobject = document.getElementById("dropdownBranch").getElementsByTagName("option");
selectobject[3].disabled = true;
$('#dropdownBranch').selectpicker('refresh');
}
Yo. Don't you just $.("selectedSelect").css("disabled", "disabled");
Okay. I apologize for my lack of effort. I realize my answer was flippant, and I chose to work on it more. Edit:
This is what I have come across on your jsfiddle. I am getting errors if I use .css, .val, .prop, or .attr. This is making me think that either jquery isn't working properly on jsfiddle for me, or I'm doing something wrong.
I dug deeper. I looked at the html. In jsfiddle, a bootstrap combobox is created above the select tag. I edited the combobox li with the value of 3 to have the class "disabled", and the desired result was obtained.
This led me to this code:
function disableDropdown(){
var selectobject, optionList;
selectobject=document.getElementById("dropdownBranch")
.getElementsByTagName("option");
selectobject = $("li");
selectobject[3].addClass("disabled");
selectobject=document.getElementById("pureDropDown")
.getElementsByTagName("option");
for(z=0;z<selectobject.length;z++){
selectobject[z].disabled=true;
}
}
What you need to do is access the bootstrap created element, and modify it to have the class disabled. Bootstrap should take care of the rest. I believe that you can do it in your own environment, but jsfiddle is annoying so I'm not going to continue working on this.
Try this
function disableDropdown(){
var selectobject;
selectobject=document.getElementById("dropdownBranch");
selectobject.options[3].disabled=true;
selectobject=document.getElementById("pureDropDown").getElementsByTagName("option");
for(z=0;z<selectobject.length;z++){
selectobject[z].disabled=true;
}
}