passing parameters to jQuery's select2 ajax call passing parameters to jQuery's select2 ajax call ajax ajax

passing parameters to jQuery's select2 ajax call


Assuming there are multiple elements with class auto-sug, you could try something like this:

$(".auto-sug").each(function() {    var thisId = this.id;    $(this).select2({        ...        ajax: {            ...            id: thisId,        },    });});


You should pass that extra parameter inside the data function instead of the root ajax, for it to execute each time you make a request:

ajax: {    url: "/action/get-custom.php",    data: function (term, page) {      return {          q: term, // search term          anotherParm: whatEverValue, //Get your value from other elements using Query, for example.          page_limit: 10      };

Then for getting the id for the current select2 you could replace whateverValue with $(this).data(key)


You can add a new params here.

 data: function (params) {        ultimaConsulta = params.term;        localidad = $("#idOcultoLocalidad").val(); //this is the anotherParm        return {            criterio: params.term, // search term            criterio2: localidad,        };    },