Skip to content

Instantly share code, notes, and snippets.

@chrishoage
Created April 3, 2012 09:35
Show Gist options
  • Save chrishoage/2290692 to your computer and use it in GitHub Desktop.
Save chrishoage/2290692 to your computer and use it in GitHub Desktop.
Add Item inline for Chosen
$("form").bind("liszt:ready", function(event, chosen) {
var chooseInstance = chosen.chosen,
$chosenDrop = $(chooseInstance.search_results).parent();
var $addHtml = $("<div/>", {
html: $("<input/>", {
type: "text",
keypress: function(e) {
if(e.which == 13) {
e.preventDefault();
var $this = $(this),
val = $this.val().trim();
$(event.target).append($("<option/>", {
text: val,
value: val.replace(" ", "_")
}))
chooseInstance.form_field_jq.trigger("liszt:updated");
$this.val('').blur()
var $resultsDiv = $chosenDrop.find(".chzn-results");
$resultsDiv.scrollTop($resultsDiv[0].scrollHeight);
chooseInstance.result_do_highlight($resultsDiv.children("li").last())
}
}
}),
class: "add-item"
});
$chosenDrop.append($addHtml)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment