I have solved my problem using below code.So may be helpful for someone.I have tried by reference style for autocomplete
<script>
var locations="<?php echo $json;?>";
var locations_array = JSON.parse(locations);
locations_array.sort();
function monkeyPatchAutocomplete2() {
jQuery("#result").remove();
// Don't really need to save the old fn,
// but I could chain if I wanted to
var oldFn = jQuery.ui.autocomplete.prototype._renderItem;
jQuery.ui.autocomplete.prototype._renderItem = function( ul, item) {
var re = new RegExp( "\\b" + this.term, "i") ;
var t = item.label.replace(re,"<span class="highlighted">" + this.term + "</span>");
return jQuery( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + t + "</a>" )
.appendTo( ul );
};
}
jQuery(document).ready(function() {
jQuery('.medication input').live('keyup', function (event) {
if(this.value.length < 2){
jQuery("#result").remove();
}
monkeyPatchAutocomplete2();
//jQuery("#result").remove();
jQuery( ".medication input" ).autocomplete({
matchContains: true,
multiselect: true,
minLength: 2,
source: function(req, responseFn) {
//addMessage("search on: '" + req.term + "'<br/>");
var re = jQuery.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp( "\\b" + re, "i" );
var a = jQuery.grep( locations_array, function(item,index){
//addMessage(" sniffing: '" + item + "'<br/>");
return matcher.test(item);
});
responseFn( a );
},
focus: function (event, ui) {
jQuery("#result").remove();
return false;
},
});
});
</script>