Skip to content

Commit b58ae98

Browse files
committed
converted to a plugin
1 parent 5bb3da7 commit b58ae98

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

index.html

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,12 @@
99

1010
<script type="text/javascript" src="jquery-1.3.1.min.js"></script>
1111
<script type="text/javascript" src="jquery.autocomplete.js"></script>
12+
<script type="text/javascript" src="jquery.select-autocomplete.js"></script>
1213
<script type="text/javascript">
1314
$(document).ready(function() {
14-
init_autocomplete_selects();
15+
$('select.autocomplete').select_autocomplete();
1516
});
16-
17-
function init_autocomplete_selects() {
18-
//iterate over each autocomplete select
19-
$('select.autocomplete').each(function() {
20-
//stick each of it's options in to an items array of objects with name and value attributes
21-
var select = this;
22-
var items = [];
23-
$(select).children('option').each(function(){
24-
var item = $(this);
25-
if (item.val() != '') //ignore empty value options
26-
{
27-
var name = item.html();
28-
var value = item.val();
29-
items.push( {'name':name, 'value':value} );
30-
}
31-
});
32-
33-
//make a new input box next to the select list
34-
var input = $("<input type='text' />").appendTo('body');
35-
$(select).after(input);
36-
37-
//make the input box into an autocomplete for the select items
38-
$(input).autocomplete(items, {
39-
data: items,
40-
minChars: 0,
41-
width: 310,
42-
matchContains: false,
43-
autoFill: false,
44-
formatItem: function(row, i, max) {
45-
return row.name;
46-
},
47-
formatMatch: function(row, i, max) {
48-
return row.name;
49-
},
50-
formatResult: function(row) {
51-
return row.name;
52-
}
53-
});
54-
55-
//make the result handler set the selected item in the select list
56-
$(input).result(function(event, selected_item, formatted) {
57-
var to_be_selected = $(select).find("option[value=" + selected_item.value + "]")[0];
58-
$(to_be_selected).attr('selected', 'selected');
59-
});
60-
61-
//set the initial text value of the autocomplete input box to the text node of the selected item in the select control
62-
$(input).val($(select).find(':selected').text());
63-
64-
//normally, you'd hide the select list but we won't for this demo
65-
//$(select).hide();
66-
});
67-
}
17+
6818
</script>
6919
</head>
7020

jquery.select-autocomplete.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
jQuery.fn.select_autocomplete = function() {
2+
return this.each(function(){
3+
if (this.tagName.toLowerCase() != 'select') { return; }
4+
5+
//stick each of it's options in to an items array of objects with name and value attributes
6+
var select = this;
7+
var items = [];
8+
$(select).children('option').each(function(){
9+
var item = $(this);
10+
if (item.val() != '') //ignore empty value options
11+
{
12+
var name = item.html();
13+
var value = item.val();
14+
items.push( {'name':name, 'value':value} );
15+
}
16+
});
17+
18+
//make a new input box next to the select list
19+
var input = $("<input type='text' />").appendTo('body');
20+
$(select).after(input);
21+
22+
//make the input box into an autocomplete for the select items
23+
$(input).autocomplete(items, {
24+
data: items,
25+
minChars: 0,
26+
width: 310,
27+
matchContains: false,
28+
autoFill: false,
29+
formatItem: function(row, i, max) {
30+
return row.name;
31+
},
32+
formatMatch: function(row, i, max) {
33+
return row.name;
34+
},
35+
formatResult: function(row) {
36+
return row.name;
37+
}
38+
});
39+
40+
//make the result handler set the selected item in the select list
41+
$(input).result(function(event, selected_item, formatted) {
42+
var to_be_selected = $(select).find("option[value=" + selected_item.value + "]")[0];
43+
$(to_be_selected).attr('selected', 'selected');
44+
});
45+
46+
//set the initial text value of the autocomplete input box to the text node of the selected item in the select control
47+
$(input).val($(select).find(':selected').text());
48+
49+
//normally, you'd hide the select list but we won't for this demo
50+
$(select).hide();
51+
});
52+
};

0 commit comments

Comments
 (0)