Reputation: 1057
I have been working on this a while and can't seem to get anywhere. Basically I have the autocomplete on a bunch of inputs by class, but I need to get the specific inputs id to build the object to post the ajax (I have to use POST for this project not GET).
$(".input_autocomplete").autocomplete({
source: function( request, response ) {
// here is where I get the hash from local storage,
// reference the id and build the object
// tried this.id, $(this).prop('id'), no luck
$.ajax({
url: '/somepath/filename.htm',
contentType: 'application/json',
dataType: 'json',
type: 'POST',
data: JSON.stringify(obj),
success: function(json) {
return {
label: item.label,
value: item.label
}
},
error: function() {
// error handling
}
}); // ajax
} // source
});
Upvotes: 8
Views: 14473
Reputation: 2030
I just spent a while piecing together a solution for this myself. In case it helps, this works for me:
call this after your class exists ( for most this is on document ready, for myself I had to call this function after a datatable initialized MyClassName)
$('.MyClassName').each(function(index) {
MyID = $(this).attr("id")
$('#'+MyID).autocomplete({
source: function(request, response) {
var elementID = this.element[0].id //!!! This is what did the trick ***
$.ajax({
url: "/MyController/MyFunction",
dataType: "json",
data: {
MySearchTerm: $('#'+elementID).val()
},
success: function(data) {
...
Upvotes: 1
Reputation: 479
I was in a situation similar to you, user1572796
and this is what worked for me:
$(this).prop("id");
Upvotes: 3
Reputation: 581
I tried it out in a project of mine. This worked for me:
$(this.element.get(0)).attr('id');
Upvotes: 4
Reputation: 126082
Try:
$(this.element).prop("id");
or:
this.element[0].id;
Inside of the source
callback, this
refers to the widget instance. In order to get the element the widget is attached to, you should use this.element
, which is a jQuery object.
Upvotes: 15