Created
February 17, 2012 01:16
-
-
Save eddiemonge/1849494 to your computer and use it in GitHub Desktop.
jQuery Simple Add Option ComboBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.comboBox = function () { | |
var $this = $(this); | |
return $this.on("change", function(){ | |
if ( $this.val() === 'add' ) { | |
$("<input type='text' />").blur(function(e) { | |
e.preventDefault(); | |
var $i = $(this), | |
val = $i.val(), | |
selected = $this.find("option:contains('" + ucWord(val) + "')"); | |
if (!selected.length) { | |
$("<option>", { | |
value: val, | |
text: val | |
}).insertBefore($this.find("option").last()); | |
} | |
$this.val( selected.length ? selected.val() : val ); | |
$i.empty().remove(); | |
$this.show(); | |
}).insertAfter( $this.hide() ).focus(); | |
} | |
}); | |
}; | |
function ucWord( str ) { | |
return str.charAt(0).toUpperCase() + str.slice(1); | |
} | |
})(jQuery); | |
// Usage: $('select').comboBox(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment