views:

28

answers:

1

I am currently using this for a single autocomplete box (id="drug"), and it works beautifully. I need to have a second autocomplete box (id="drug2") that uses the same array as its choices. I cannot seem to get it to work at all. Every time I try to modify the code I break it on the both. I'm sure someone out there has an elegant solution that will lead me in the right direction . . .

<script type="text/javascript">
function findValue(li) {
 if( li == null ) return alert("No match!");

 // if coming from an AJAX call, let's use the DrugId as the value
 if( !!li.extra ) var sValue = li.extra[0];

 // otherwise, let's just display the value in the text box
 else var sValue = li.selectValue;

 //alert("The value you selected was: " + sValue);
}

function selectItem(li) {
 findValue(li);
}

function formatItem(row) {
 return row[0] + " (id: " + row[1] + ")";
}

function lookupLocal(){
 var oSuggest = $("#drug")[0].autocompleter;
 oSuggest.findValue();
 return false;
}

$(document).ready(function() {
 $("#drug").autocompleteArray(

  ['Acyclovir','Amikacin','Aminophylline','Amiodarone','Ampicillin','Atropine','Azithromycin','Aztreonam','Bupivacaine','Calcium Chloride','Calcium'],
  {
   delay:10,
   minChars:1,
   matchSubset:1,
   onItemSelect:selectItem,
   onFindValue:findValue,
   autoFill:true,
   maxItemsToShow:100
  }

 );
});
</script>
+2  A: 

You can use a Multiple Selector in your jQuery call and then call your autocompleteArray on that:

$(document).ready(function() {
 $("#drug1, #drug2").autocompleteArray(
  ['Acyclovir','Amikacin','Aminophylline','Amiodarone','Ampicillin','Atropine','Azithromycin','Aztreonam','Bupivacaine','Calcium Chloride','Calcium'],
  {
   delay:10,
   minChars:1,
   matchSubset:1,
   onItemSelect:selectItem,
   onFindValue:findValue,
   autoFill:true,
   maxItemsToShow:100
  }    
 );
});
tKe
Thanks, that makes sense, and even more importantly, it works perfectly!
Natan
Glad it works! Please accept the answer :)
tKe