Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing parameters to jQuery's select2 ajax call

I'm attempting to pass an extra parameter to an ajax call within select2:

  $(".auto-sug").select2({
    width:'element',
    minimumInputLength:2,
    ajax: {
        url: "/action/get-custom.php",
        data: function (term, page) {
          return {
              q: term, // search term
              page_limit: 10
          };
        },
        results: function (data, page) {
            return {results: data.stuff};
        }
    }
  });

I actually want to pass another parameter to the ajax call... the id of the element itself

<input type="text" class="auto-sug" name="custom" id="3383" />

however, I'm unable to figure out how to actually access the id of the element (3383) or any other value on the page.

like image 647
Cary Avatar asked Nov 30 '12 01:11

Cary


1 Answers

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,
        },
    });
});
like image 72
sjy Avatar answered Oct 20 '22 04:10

sjy