var inventoryItemIndex = { defaults : { products: [], selector_type : '#inventoryitemsearch-item_type', selector_name : '#inventoryitemsearch-item_name', selector_id : '#inventoryitemsearch-item_id', }, product: null, init: function(o){ this.defaults = $.extend( this.defaults, o ); this.initAutocomplete(); }, initAutocomplete : function (){ var self = this; var $input = $(self.defaults.selector_name); $input.typeahead({source : self.defaults.products, autoSelect: true, items: 20, minLength: 3, }); $input.change(function() { var current = $input.typeahead("getActive"); $("#filter_text").val(''); if (current) { // Some item from your model is active! if (current.name == $input.val()) { self.product = current; } else { self.product = null; } } else { self.product = null; } self.productChange(); }); }, productChange: function( ){ this.clearProductData(); this.setProductData(); }, clearProductData: function(){ $(this.defaults.selector_type).val(''); $(this.defaults.selector_id).val(''); }, setProductData: function(){ if ( this.product ){ $(this.defaults.selector_type).val( this.product.type); $(this.defaults.selector_id ).val( this.product.id); } } }