2
* jQuery selectbox plugin
4
* Copyright (c) 2007 Sadri Sahraoui (brainfault.com)
5
* Licensed under the GPL license and MIT:
6
* http://www.opensource.org/licenses/GPL-license.php
7
* http://www.opensource.org/licenses/mit-license.php
9
* The code is inspired from Autocomplete plugin (http://www.dyve.net/jquery/?autocomplete)
16
* - separate css style for current selected element and hover element which solve the highlight issue
18
* - Fix width when the select is in a hidden div @Pawel Maziarz
19
* - Add a unique id for generated li to avoid conflict with other selects and empty values @Pawel Maziarz
22
selectbox: function(options) {
23
return this.each(function() {
24
new jQuery.SelectBox(this, options);
30
/* pawel maziarz: work around for ie logging */
31
if (!window.console) {
39
jQuery.SelectBox = function(selectobj, options) {
41
var opt = options || {};
42
opt.inputClass = opt.inputClass || "selectbox";
43
opt.containerClass = opt.containerClass || "selectbox-wrapper";
44
opt.hoverClass = opt.hoverClass || "current";
45
opt.currentClass = opt.selectedClass || "selected"
46
opt.debug = opt.debug || false;
48
var elm_id = selectobj.id;
52
//jquery object for select element
53
var $select = $(selectobj);
54
// jquery container object
55
var $container = setupContainer(opt);
57
var $input = setupInput(opt);
58
// hide select and append newly created elements
59
$select.hide().before($input).before($container);
71
if ($container.not(':visible')) {
76
.keydown(function(event) {
77
switch(event.keyCode) {
79
event.preventDefault();
83
event.preventDefault();
88
event.preventDefault(); // seems not working in mac !
89
$('li.'+opt.hoverClass).trigger('click');
97
if ($container.is(':visible') && hasfocus > 0 ) {
98
if(opt.debug) console.log('container visible and has focus')
111
$container.append(getSelectOptions($input.attr('id'))).hide();
112
var width = $input.css('width');
113
$container.width(width);
116
function setupContainer(options) {
117
var container = document.createElement("div");
118
$container = $(container);
119
$container.attr('id', elm_id+'_container');
120
$container.addClass(options.containerClass);
125
function setupInput(options) {
126
var input = document.createElement("input");
127
var $input = $(input);
128
$input.attr("id", elm_id+"_input");
129
$input.attr("type", "text");
130
$input.addClass(options.inputClass);
131
$input.attr("autocomplete", "off");
132
$input.attr("readonly", "readonly");
133
$input.attr("tabIndex", $select.attr("tabindex")); // "I" capital is important for ie
138
function moveSelect(step) {
139
var lis = $("li", $container);
146
} else if (active >= lis.size()) {
147
active = lis.size() - 1;
150
lis.removeClass(opt.hoverClass);
152
$(lis[active]).addClass(opt.hoverClass);
155
function setCurrent() {
156
var li = $("li."+opt.currentClass, $container).get(0);
157
var ar = (''+li.id).split('_');
158
var el = ar[ar.length-1];
160
$input.val($(li).html());
165
function getCurrentSelected() {
166
return $select.val();
170
function getCurrentValue() {
174
function getSelectOptions(parentid) {
175
var select_options = new Array();
176
var ul = document.createElement('ul');
177
$select.children('option').each(function() {
178
var li = document.createElement('li');
179
li.setAttribute('id', parentid + '_' + $(this).val());
180
li.innerHTML = $(this).html();
181
if ($(this).is(':selected')) {
182
$input.val($(this).html());
183
$(li).addClass(opt.currentClass);
187
.mouseover(function(event) {
189
if (opt.debug) console.log('over on : '+this.id);
190
jQuery(event.target, $container).addClass(opt.hoverClass);
192
.mouseout(function(event) {
194
if (opt.debug) console.log('out on : '+this.id);
195
jQuery(event.target, $container).removeClass(opt.hoverClass);
197
.click(function(event) {
198
var fl = $('li.'+opt.hoverClass, $container).get(0);
199
if (opt.debug) console.log('click on :'+this.id);
200
$('li.'+opt.currentClass).removeClass(opt.currentClass);
201
$(this).addClass(opt.currentClass);