Skip to content

Instantly share code, notes, and snippets.

@rodrigo-x
Created October 15, 2012 00:24
Show Gist options
  • Save rodrigo-x/3890266 to your computer and use it in GitHub Desktop.
Save rodrigo-x/3890266 to your computer and use it in GitHub Desktop.
snippet - add input...
$(function(){
$.options = (function(){
var $self = $(this);
if ($self.val() === "add"){
var $select = $self.closest("select");
$select.hide();
var $input = $("<input>").blur(function(){
var $self = $(this);
var newVal = $.trim($self.val());
if (newVal.length > 11) alert("Grande demais!");
else if (newVal.length == 0) return;
else $select.append("<option>" + newVal + "</option>");
$select.show();
$self.remove();
});
$select.after($input);
}
});
$("select").change($.options);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment