function split( val ) { return val.split( /[,;]\s*/ ); } function extractLast( term ) { return split( term ).pop(); } $(document).ready(function() { var ac_models = []; $("#btnModelSuggestOpen").click(function() { $('#gbModelSuggest').css('display', 'block'); if (!ac_models || !ac_models.length) { $.post('/reportModel.php', { act: 'getModels' }, function(data) { ac_models = data.split(','); //alert(ac_models.length); $('#edtModelName') .bind( "keydown", function( event ) { if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) { event.preventDefault(); } }) .autocomplete({ minLength: 1, focus: function() { return false; }, source: function( request, response ) { response( $.ui.autocomplete.filter( ac_models, extractLast( request.term ) ) ); }, select: function( event, ui ) { var terms = split( this.value ); terms.pop(); terms.push( ui.item.value ); this.value = terms.join( ", " ); return false; } }); }); } $('#edtModelName').val(''); return false; }); $("#btnModelSuggestSubmit").click(function() { if ($("#edtModelName").val().length < 4) { alert('Please,enter model name first'); return; } $.post('/reportModel.php', { id : '190066', models : $("#edtModelName").val() }, function(data) { $('#gbModelSuggest').css('display', 'none'); data == 'OK' ? alert('Model successfully reported') : alert('Error reporting model'); }); }); });