I have an input box similar to this http://jqueryui.com/demos/autocomplete/#multiple with the add button. I want to add all values split by comma added to a list on enter. I can't manage to do this because the keypress with enter seems to be conflict with select
event of autocomplete. The problem is that the user can add only one item. I want the user to add multiple items then press enter key to add them to the list at the same time.
The enter key event should happen only when the suggestion list of autocomplete is closed.
You can use the open
and close
events of autocomplete
to keep track whether or not the suggestion list is currently open (storing this information somewhere - in the example below, in the "selectVisible"
data
):
$( "#tags" )
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.ENTER && !$(this).data("selectVisible") ) {
// Your code
}
...
})
.autocomplete({
open: function() {
$(this).data("selectVisible", true);
},
close: function() {
$(this).data("selectVisible", false);
},
...
});
Working example at jsFiddle.
I've used the jQuery Multiselect widget with great success before and I believe it is more intuitive to the user that they can select multiple options.
Also it handles the Enter key appropriately out of the box.
http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With