Vyshnavi Samudrala Vyshnavi Samudrala - 3 months ago 21
jQuery Question

If condition inside jquery ui autocomplete

$(function() {
var services = [
"Water Wash",
"Tyre Puncture"
];
var servicesCar = [
"Car Wash",
"Tyre Puncture",
"Vehicle Diagonstics",
"Other Repairs",
];
$('#ServiceType').autocomplete({
if($('#VehicleType').val() == '4w') {
source: servicesCar,
minLength: 0,
scroll: true
}
else {
source: services,
minLength: 0,
scroll: true
} }).focus(function() {
$(this).autocomplete("search", "");
});
});


I need to check whether the option value is
2w
or
4w
and based on that value, I should display the source tags. But, this code is not autocompleting the input

<select id="VehicleType" name="VehicleType">
<option value="2w"> Bike </option>
<option value="4w"> Car </option>
</select>


Here is my HTML code

Answer

HTML

<select id="VehicleType" name="VehicleType">
  <option value="2w"> Bike </option> 
  <option value="4w"> Car </option> 
</select>

<input type="text" id="ServiceType" /> 

JS

$(function() {
var services = [      
  "Water Wash",
  "Tyre Puncture"      
];
var servicesCar = [      
  "Car Wash",
  "Tyre Puncture",
  "Vehicle Diagonstics",
  "Other Repairs",
];

$( "#ServiceType" ).focusin(function() {
        if($('#VehicleType').val() == '4w') {
                $('#ServiceType').autocomplete({
            source: servicesCar,
            minLength: 0,
            scroll: true
        });        
   }
  else {
            $('#ServiceType').autocomplete({
            source: services,
            minLength: 0,
            scroll: true
        });  
    }    
        });
});

Reference Fiddle