Autocompleting when a certain class is present This jQuery actually autocomplete when the onkeydown class is present and load the autocomplete with corresponding variable. This is working but I want to make it shorter.

var autocomp_opt = {
    source: "&lt;?php echo base_url();?&gt;description/get_description",
    minLength: 1,
    select: function(evt, ui) {
        this.form.sub_description.value = ui.item.sub_description;
    }
};

var autocomp_opt1 = {
    source: "&lt;?php echo base_url();?&gt;description/get_description",
    minLength: 1,
    select: function(evt, ui) {
        this.form.sub_description2.value = ui.item.sub_description;
    }
};

$(document).on("keydown", ".sn", function () { 
    $(this).autocomplete(autocomp_opt);
});

$(document).on("keydown", ".sn2", function () { 
    $(this).autocomplete(autocomp_opt1);
});
I'm proposing to reduce code by putting all similar code to one function:

function initAutocomplete(selector, field) {
  $(selector).on('keydown', function() { 
    $(this).autocomplete({
      source: '&lt;?php echo base_url();?&gt;description/get_description',
      minLength: 1,
      select: function(evt, ui) {
        this.form.elements[field].value = ui.item.sub_description;
      }
    });
  });
}

initAutocomplete('.sn', 'sub_description');
initAutocomplete('.sn2', 'sub_description2');