I am using jQuery v1.8.3 and jQuery UI v1.9.2. I have implemented the Autocomplete widget this way:
$('#input_id').autocomplete({
create: function (event, ui) {
// Initialize data
$(this).data( 'custom', { property1: 'Hello', property2: { num: 1, funct: function() { ... return value } } );
alert($(this).data('custom').property1) // Display 'Hello'
},
select: function(event, ui) {
alert($(this).data('custom').property1) // Display 'Hello'
},
source: function(request, response) {
alert($(this).data('custom').property1) // Display 'undefined'
alert(this.data('custom').property1) // I get 'TypeError: this.data is not a function'
}
});
Why in the source option I get undefined while in create and select events I get Hello? How should I properly access the number property in the search option context so to get Hello?
You’re getting undefined there, because apparently
thisinsidesourcefunction refers to anonymous function, not to the INPUT you’re assigning data to increatefunction.Use other means to access input inside
sourcefunction.To access your data within source function use following:
Demo for future quick reference: http://jsbin.com/ojesig/1/edit