Reputation: 837
I wrote a jQuery autocomplete code in my application, it works properly but now I want it to autocomplete only after minimum length
is reached e.g. minlength=2
and in if condition if((('#id').val())>minlength)
I tried following code
var minlength = 2;
$(document).ready(function () {
var text = $('#patient_id').val();
$('#patient_id').autocomplete({
//var text = $(this).val();
if(text > minlength){
source: function( request, response ) {
$.ajax({
url : 'ipdpatientajax.php',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'country_table',
row_num : 1
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
console.log(names[1], names[2], names[3]);
$('#patientAddress').val(names[1]);
$('#patientSex').val(names[2]);
$('#patientAge').val(names[3]);
$('#dateadmit_id').val(names[4]);
$('#datedischarge_id').val(names[5]);
$("#patientAddress").attr("readonly", "readonly");
$("#patientSex").attr("readonly", "readonly");
$("#patientAge").attr("readonly", "readonly");
$("#patient_id").attr("readonly", "readonly");
$("#dateadmit_id").attr("readonly", "readonly");
$("#datedischarge_id").attr("readonly", "readonly");
}
}
});
/* other code continues.....}
but this gives me error that' Uncaught SyntaxError: Unexpected token >
'
Upvotes: 0
Views: 184
Reputation: 466
I can't test the code right now, but on the first sight you are forgetting to get the length of the text. So the if-clause should be:
if(text.length > minlength){
Edit:
And your if needs to be before the autocomplete call. Right now it is within the brackets for the options.
Furthermore you have to track when the minlength is reached. You will need an onChange event handler.
$('#patient_id').on('change', function() {
var text = $('#patient_id').val();
if(text.length > minlength){
//var text = $(this).val();
$('#patient_id').autocomplete({
...
})
Upvotes: 1