Created
April 3, 2012 09:35
-
-
Save chrishoage/2290692 to your computer and use it in GitHub Desktop.
Add Item inline for Chosen
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
$("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