Select Option Custom Tag JQUERY
I have multiple select options on many of my pages, I just just added a jquery filter plugin to one of the select options i.e As User types on an input box the values in the select options gets filtered. This works very well now. How can I make it this into a custom Tag where it can be resused by all the select options on the site. That is the id of the select option and the input box has to be passed to the jquery to automtically filter the content of the Select Option. below is my jquery script.
<div>Filter<br>
<input id="txtcomm" type="text" name="">
</div>
<div>
<select id="slctcomm" multiple style="width:220px">
<cfloop query="qryobjComm">
<option value="#Comm#">#Comm#</option>
</cfloop>
</select>
</div>
<script>
jQuery.fn.filterByText = function(txtcomm, selectSingleMatch) {
return this.each(function() {
var slctcomm = this;
var options = [];
$(slctcomm).find('option').each(function() {
options.push({value: $(this).val(), text: $(this).text()});
});
$(slctcomm).data('options', options);
$(txtcomm).bind('change keyup', function() {
var options = $(slctcomm).empty().data('options');
var search = $.trim($(this).val());
var regex = new RegExp(search,'gi');
$.each(options, function(i) {
var option = options;
if(option.text.match(regex) !== null) {
$(slctcomm).append(
$('<option>').text(option.text).val(option.value)
);
}
});
if (selectSingleMatch === true &&
$(slctcomm).children().length === 1) {
$(slctcomm).children().get(0).selected = true;
}
});
});
};
$(function() {
$('#slctcomm').filterByText($('#txtcomm'), true);
});
</script>
