Select2 how to enable local search after remote data retrieved? - jquery-select2

I have two select2 controllers: Province and District. District is getting its data from server based Province ID. What I want is once District data has been retrieved, and when typing in search box in select2 of District, I don't want it to make an ajax request to server again, I want it to search in the existing data of District only.
Here what I have:
<select class="form-control" id="Province" name="Province">
<option value="__None">--Choose Province--</option>
<option value="01" selected="selected">Banteay Meanchey</option>
<option value="02">Battambang</option>
<option value="03">Kampong Cham</option>
<option value="04">Kampong Chhnang</option>
<option value="05">Kampong Speu</option>
<option value="06">Kampong Thom</option>
<option value="07">Kampot</option>
<option value="08">Kandal</option>
</select>
<select class="form-control" name="District" id="District"></select>
<script type="text/javascript">
$('select').select2()
$('#District').replaceWith('<input class="form-control" id="District" name="District">')
$('#District').select2({
ajax:{
url:'localhost/getDistrict?ProvinceID='+$('#Province').val(),
dataType: 'json',
data: function (term, page) {
return {
ProvinceID: $('#Province').val(), // search term
};
},
results: function (data, page) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter the remote JSON data
var results = [];
var keys = Object.keys(data.results)
for(var i=0;i<keys.length;i++){
results.push({
'id':keys[i],
'text':data.results[keys[i]]
})
}
return { results: results };
},
cache: true
}
})
</script>
How can I enable local search in Select2 District Controller, or any select2 option that enable local search ? Thanks

Related

select2 - Get the value of the clicked box

In my Select2 multi-select boxes I want to be able to click on the selected boxes and getting their corresponding option value (or object).
This is (in a nutshell) what I have so far:
$('#mySelect2').next('span').find('ul').on('click', '.select2-selection__choice', function (e) {
console.log($('#mySelect2').select2('data'));
alert($(this).data('select2-id'));
});
I searched the select2 data objects but there is nothing related to the clicked box and the select2-id data attribute
seems to be random.
Where can I find some matching id ?
Can try the below code, use select2 - select and unselect event.
$("#mySelect2").on("select2:select select2:unselect", function (e) {
//get all items by, for multiple an array like ["9", "20"] will be returned
var items= $(this).val();
//Last selected value
var lastItem = e.params.data.id;
})
I think I've found a workaround solution.
It seems that select2 uses the title attribute in the rending list.
So the trick is to use this attribute as an id in each option.
<select multiple id="states" name="states[]" class="select2">
<option title="states-AL" value="AL">Alabama</option>
<option title="states-CA" value="CA">California</option>
<option title="states-MI" value="MI">Michigan</option>
<option title="states-UT" value="UT">Utah</option>
<option title="states-WY" value="WY">Wyoming</option>
</select>
$('#states').next('span').find('ul').on('click', '.select2-selection__choice', function (e) {
alert($(this).attr('title'));
});

How to retrieve data attribute from select options of a form select menu using stimulus? [duplicate]

How do I get the selected value from a dropdown list using JavaScript?
<form>
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
</form>
Given a select element that looks like this:
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
Running this code:
var e = document.getElementById("ddlViewBy");
var value = e.value;
var text = e.options[e.selectedIndex].text;
Results in:
value == 2
text == "test2"
Interactive example:
var e = document.getElementById("ddlViewBy");
function onChange() {
var value = e.value;
var text = e.options[e.selectedIndex].text;
console.log(value, text);
}
e.onchange = onChange;
onChange();
<form>
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
</form>
Plain JavaScript:
var e = document.getElementById("elementId");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;
jQuery:
$("#elementId :selected").text(); // The text content of the selected option
$("#elementId").val(); // The value of the selected option
AngularJS: (http://jsfiddle.net/qk5wwyct):
// HTML
<select ng-model="selectItem" ng-options="item as item.text for item in items">
</select>
<p>Text: {{selectItem.text}}</p>
<p>Value: {{selectItem.value}}</p>
// JavaScript
$scope.items = [{
value: 'item_1_id',
text: 'Item 1'
}, {
value: 'item_2_id',
text: 'Item 2'
}];
var strUser = e.options[e.selectedIndex].value;
This is correct and should give you the value.
Is it the text you're after?
var strUser = e.options[e.selectedIndex].text;
So you're clear on the terminology:
<select>
<option value="hello">Hello World</option>
</select>
This option has:
Index = 0
Value = hello
Text = Hello World
The following code exhibits various examples related to getting/putting of values from input/select fields using JavaScript.
Source Link
Working Javascript & jQuery Demo
<select id="Ultra" onchange="run()"> <!--Call run() function-->
<option value="0">Select</option>
<option value="8">text1</option>
<option value="5">text2</option>
<option value="4">text3</option>
</select><br><br>
TextBox1<br>
<input type="text" id="srt" placeholder="get value on option select"><br>
TextBox2<br>
<input type="text" id="rtt" placeholder="Write Something !" onkeyup="up()">
The following script is getting the value of the selected option and putting it in text box 1
<script>
function run() {
document.getElementById("srt").value = document.getElementById("Ultra").value;
}
</script>
The following script is getting a value from a text box 2 and alerting with its value
<script>
function up() {
//if (document.getElementById("srt").value != "") {
var dop = document.getElementById("srt").value;
//}
alert(dop);
}
</script>
The following script is calling a function from a function
<script>
function up() {
var dop = document.getElementById("srt").value;
pop(dop); // Calling function pop
}
function pop(val) {
alert(val);
}?
</script>
var selectedValue = document.getElementById("ddlViewBy").value;
If you ever run across code written purely for Internet Explorer you might see this:
var e = document.getElementById("ddlViewBy");
var strUser = e.options(e.selectedIndex).value;
Running the above in Firefox et al will give you an 'is not a function' error, because Internet Explorer allows you to get away with using () instead of []:
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
The correct way is to use square brackets.
Use:
<select id="Ultra" onchange="alert(this.value)">
<option value="0">Select</option>
<option value="8">text1</option>
<option value="5">text2</option>
<option value="4">text3</option>
</select>
Any input/form field can use a “this” keyword when you are accessing it from inside the element. This eliminates the need for locating a form in the DOM tree and then locating this element inside the form.
There are two ways to get this done either using JavaScript or jQuery.
JavaScript:
var getValue = document.getElementById('ddlViewBy').selectedOptions[0].value;
alert (getValue); // This will output the value selected.
OR
var ddlViewBy = document.getElementById('ddlViewBy');
var value = ddlViewBy.options[ddlViewBy.selectedIndex].value;
var text = ddlViewBy.options[ddlViewBy.selectedIndex].text;
alert (value); // This will output the value selected
alert (text); // This will output the text of the value selected
jQuery:
$("#ddlViewBy:selected").text(); // Text of the selected value
$("#ddlViewBy").val(); // Outputs the value of the ID in 'ddlViewBy'
Beginners are likely to want to access values from a select with the NAME attribute rather than ID attribute. We know all form elements need names, even before they get ids.
So, I'm adding the getElementsByName() solution just for new developers to see too.
NB. names for form elements will need to be unique for your form to be usable once posted, but the DOM can allow a name be shared by more than one element. For that reason consider adding IDs to forms if you can, or be explicit with form element names my_nth_select_named_x and my_nth_text_input_named_y.
Example using getElementsByName:
var e = document.getElementsByName("my_select_with_name_ddlViewBy")[0];
var strUser = e.options[e.selectedIndex].value;
Just use
$('#SelectBoxId option:selected').text(); for getting the text as listed
$('#SelectBoxId').val(); for getting the selected index value
I don't know if I'm the one that doesn't get the question right, but this just worked for me:
Use an onchange() event in your HTML, for example.
<select id="numberToSelect" onchange="selectNum()">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
JavaScript
function selectNum() {
var strUser = document.getElementById("numberToSelect").value;
}
This will give you whatever value is on the select dropdown per click.
Using jQuery:
$('select').val();
The previous answers still leave room for improvement because of the possibilities, the intuitiveness of the code, and the use of id versus name. One can get a read-out of three data of a selected option -- its index number, its value and its text. This simple, cross-browser code does all three:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo GetSelectOptionData</title>
</head>
<body>
<form name="demoForm">
<select name="demoSelect" onchange="showData()">
<option value="zilch">Select:</option>
<option value="A">Option 1</option>
<option value="B">Option 2</option>
<option value="C">Option 3</option>
</select>
</form>
<p id="firstP"> </p>
<p id="secondP"> </p>
<p id="thirdP"> </p>
<script>
function showData() {
var theSelect = demoForm.demoSelect;
var firstP = document.getElementById('firstP');
var secondP = document.getElementById('secondP');
var thirdP = document.getElementById('thirdP');
firstP.innerHTML = ('This option\'s index number is: ' + theSelect.selectedIndex + ' (Javascript index numbers start at 0)');
secondP.innerHTML = ('Its value is: ' + theSelect[theSelect.selectedIndex].value);
thirdP.innerHTML = ('Its text is: ' + theSelect[theSelect.selectedIndex].text);
}
</script>
</body>
</html>
Live demo: http://jsbin.com/jiwena/1/edit?html,output .
id should be used for make-up purposes. For functional form purposes, name is still valid, also in HTML5, and should still be used. Lastly, mind the use of square versus round brackets in certain places. As was explained before, only (older versions of) Internet Explorer will accept round ones in all places.
Another solution is:
document.getElementById('elementId').selectedOptions[0].value
The simplest way to do this is:
var value = document.getElementById("selectId").value;
You can use querySelector.
E.g.
var myElement = document.getElementById('ddlViewBy');
var myValue = myElement.querySelector('[selected]').value;
Running example of how it works:
var e = document.getElementById("ddlViewBy");
var val1 = e.options[e.selectedIndex].value;
var txt = e.options[e.selectedIndex].text;
document.write("<br />Selected option Value: "+ val1);
document.write("<br />Selected option Text: "+ txt);
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3" selected="selected">test3</option>
</select>
Note: The values don't change as the dropdown is changed, if you require that functionality then an onClick change is to be implemented.
To go along with the previous answers, this is how I do it as a one-liner. This is for getting the actual text of the selected option. There are good examples for getting the index number already. (And for the text, I just wanted to show this way)
let selText = document.getElementById('elementId').options[document.getElementById('elementId').selectedIndex].text
In some rare instances you may need to use parentheses, but this would be very rare.
let selText = (document.getElementById('elementId')).options[(document.getElementById('elementId')).selectedIndex].text;
I doubt this processes any faster than the two line version. I simply like to consolidate my code as much as possible.
Unfortunately this still fetches the element twice, which is not ideal. A method that only grabs the element once would be more useful, but I have not figured that out yet, in regards to doing this with one line of code.
I have a bit different view of how to achieve this. I'm usually doing this with the following approach (it is an easier way and works with every browser as far as I know):
<select onChange="functionToCall(this.value);" id="ddlViewBy">
<option value="value1">Text one</option>
<option value="value2">Text two</option>
<option value="value3">Text three</option>
<option value="valueN">Text N</option>
</select>
In 2015, in Firefox, the following also works.
e.options.selectedIndex
In more modern browsers, querySelector allows us to retrieve the selected option in one statement, using the :checked pseudo-class. From the selected option, we can gather whatever information we need:
const opt = document.querySelector('#ddlViewBy option:checked');
// opt is now the selected option, so
console.log(opt.value, 'is the selected value');
console.log(opt.text, "is the selected option's text");
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
event.target.value inside the onChange callback did the trick for me.
Most answers here get the value of the "this" select menu onchange by a plain text JavaScript selector.
For example:
document.getElementById("ddlViewBy").value;
This is not a DRY approach.
DRY (three lines of code):
function handleChange(e) {
let innerText = e.target[e.target.options.selectedIndex].innerText;
let value = e.target.value;
/* Do something with these values */
}
Get the first select option:
console.log(e.target[0]); /* Output: <option value="value_hello">Hello innerText</option>*/
With this idea in mind, we dynamically return a "this" select option item (by selectedIndex):
e.target[e.target.options.selectedIndex].innerText;
Demo
let log = document.getElementById('log');
function handleChange(e) {
let innerText = e.target[e.target.options.selectedIndex].innerText;
let value = e.target.value;
log.innerHTML = `<table>
<tr><th>value</th><th>innerText</th></tr>
<tr><td>${value}</td><td>${innerText}</td></tr>
</table>`;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.css">
<select id="greet" onchange="handleChange(event)">
<option value="value_hello">Hello innerText</option>
<option value="value_goodbye">Goodbye innerText</option>
<option value="value_seeYou">See you... innerText</option>
</select>
<select id="other_select_menu" onchange="handleChange(event)">
<option value="value_paris">Paris innerText</option>
<option value="value_ny">New York innerText</option>
</select>
<div id="log"></div>
Here is a JavaScript code line:
var x = document.form1.list.value;
Assuming that the dropdown menu named list name="list" and included in a form with name attribute name="form1".
I think you can attach an event listener to the select tag itself e.g:
<script>
document.addEventListener("DOMContentLoaded", (_) => {
document.querySelector("select").addEventListener("change", (e) => {
console.log(e.target.value);
});
});
</script>
In this scenario, you should make sure you have a value attribute for all of your options, and they are not null.
You should be using querySelector to achieve this. This also standardizes the way of getting a value from form elements.
var dropDownValue = document.querySelector('#ddlViewBy').value;
Fiddle: https://jsfiddle.net/3t80pubr/
Try
ddlViewBy.value // value
ddlViewBy.selectedOptions[0].text // label
console.log( ddlViewBy.value );
console.log( ddlViewBy.selectedOptions[0].text );
<select id="ddlViewBy">
<option value="1">Happy</option>
<option value="2">Tree</option>
<option value="3" selected="selected">Friends</option>
</select>
Here's an easy way to do it in an onchange function:
event.target.options[event.target.selectedIndex].dataset.name
<select name="test" id="test" >
<option value="1" full-name="Apple">A</option>
<option value="2" full-name="Ball">B</option>
<option value="3" full-name="Cat" selected>C</option>
</select>
var obj = document.getElementById('test');
obj.options[obj.selectedIndex].value; //3
obj.options[obj.selectedIndex].text; //C
obj.options[obj.selectedIndex].getAttribute('full-name'); //Cat
obj.options[obj.selectedIndex].selected; //true
There is a workaround, using the EasyUI framework with all of its plugins.
You only need to add some EasyUI object that can read from an input as a "bridge" to the drop-down menu.
Example: easyui-searchbox
To the left, the drop-down, to the right, the easyui-searchbox:
...
<input id="ss" class="easyui-searchbox" style="width:300px"
data-options=" searcher:my_function,
prompt:'Enter value',
menu:'#mm'">
<div id="mm" style="width:200px">
<div data-options="name:'1'">test1</div>
<div data-options="name:'2'">test2</div>
</div>
...
...
<script type="text/javascript">
function my_js_function(triggeredByButton = false){
// normal text of the searchbox (what you entered)
var value = $("#ss").searchbox("getValue");
// what you chose from the drop-down menu
var name = $("#ss").searchbox("getName");
...
Mind: the var name is the '1' or '2', that is, the "value of the drop-down", while var value is the value that was entered in the easyui-searchbox instead and not relevant if you only want to know the value of the drop-down.
I checked how EasyUI fetches that #mm name, and I could not find out how to get that name without the help of EasyUI. The jQuery behind getName:
getName:function(jq){
return $.data(jq[0],"searchbox").searchbox.find("input.textbox-value").attr("name");
}
Mind that the return of this function is not the value of the easyui-searchbox, but the name of the #mm drop-down that was used as the menu parameter of the easyui-searchbox. Somehow EasyUI must get that other value, therefore it must be possible.
If you do not want any plugin to be seen, make it as tiny as possible? Or find perhaps a plugin that does not need a form at all in the link above, I just did not take the time.

Select2 with tags and suggestions

I was using Select2 to have a list of brands and categories, each grouped by <optgroup>.
So far so good, because the list was not changing. Now I have to implement a Product search, which will have no specific values, so you can search for everything you type in.
For example, if the user types in "sho", i want to have:
**Products**
- sho (exactly what the user enters)
**Brands** (suggestions from a list)
- Adidas Shoes
- Nike Shoes
**Categories** (suggestions from a list)
- Shoes for women
- Shoes for men
And as the user types more characters, the sho under Products keeps updating to show what they entered.
The code for the previous version was:
<select id="search_bar">
<option></option>
<optgroup label="Stores">
{% for merchant in all_merchants %}
<option value="{% url %}">{{ merchant.display_name }}</option>
{% endfor %}
</optgroup>
<optgroup label="Categories">
{% for category in all_categories %}
<option value="{% url %}">{{ category.name }}</option>
{% endfor %}
</optgroup>
</select>
I tried enabling tags, by doing tags: true, and it works but not entirely, because if I type in something that matches a brand or a category, the tag will disappear. I.E, if i type "Adidas", it will show only the Adidas under brands (so if i click it, I'm taken to the brand page), but not as a tag that i can click and be taken to the product search page.
Does that make sense?
Ok its first try, i you click on new tag to create, i keep it in the list of firstdropdown and you have propagation in the second dropdown (at least 2 character minimun):
$('#source').select2({
tags:true
});
$('#search').select2({
tags:true,
insertTag:function (data, tag) {
data.push(tag);
},
createTag: function (params) {
return {
id: params.term,
text: params.term,
newTag: true
}
},
matcher: matchCustom,
}).on("select2:select", function(e) {
// if( e.params.data.newTag){
select2_search($("#source"), e.params.data.text);
$(this).find('[value="'+e.params.data.id+'"]').replaceWith('<option selected value="'+e.params.data.id+'">'+e.params.data.text+'</option>');
// }
//uncomment if you want to play the same search
//$("#search").val("0").trigger("change");
});
function matchCustom(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') {
return null;
}
// Return `null` if the term should not be displayed
return null;
}
function select2_search ($el, term) {
$el.select2('open');
// Get the search box within the dropdown or the selection
// Dropdown = single, Selection = multiple
var $search = $el.data('select2').dropdown.$search || $el.data('select2').selection.$search;
// This is undocumented and may change in the future
$search.val(term);
$search.trigger('input');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/js/select2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
<select id ="search" class="myselect" style="width:50%;" >
<option value="0"></option>
</select> <prev><== Type new tag into search</prev>
<br><br><br><br><br><br><br><br>
<select style="width:100%;" id="source" multiple="" tabindex="-1" aria-hidden="true">
<optgroup class="select2-result-selectable" label="Brand" >
<option value="Adidas shoes">Adidas shoes</option>
<option value="Pantalon">Pantalon</option>
<option value="short">short</option>
</optgroup>
<optgroup class="select2-result-selectable" label="Categories" >
<option value="Shoes for men">Shoes for men</option>
<option value="Shoes for women">Shoes for women</option>
<option value="Other things for wommen">Other things for women</option>
<option value="Other things for men">Other things for men</option>
</optgroup>
</select>

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