0
$(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

4
  • Please mention the issue you encountered Commented Aug 22, 2016 at 11:26
  • @AnoopLL It is not autocompleting with an if condition inside it, Whereas, I need to check the condition and then send source tags Commented Aug 22, 2016 at 11:28
  • you need to write the autocomplete code inside the onChange of vehicle type. Commented Aug 22, 2016 at 11:30
  • I don't need to call autocomplete on changing the option. I just have to take the current value being selected and send tags based upon that @AnoopLL Commented Aug 22, 2016 at 11:34

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.