Skip to content

Commit 41543d9

Browse files
author
Tomas Kirda
committed
Return instance of Autocomplete for the first matched element if called without arguments.
1 parent c650a51 commit 41543d9

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

spec/autocompleteBehavior.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,16 @@ describe('Autocomplete', function () {
373373
expect(input.data('autocomplete')).toBeUndefined();
374374
expect(div.children().length).toBe(0);
375375
});
376+
377+
it('Should return Autocomplete instance if called without arguments', function () {
378+
var input = $(document.createElement('input'));
379+
380+
input.autocomplete({
381+
serviceUrl: '/test-dispose'
382+
});
383+
384+
var instance = input.autocomplete();
385+
386+
expect(instance instanceof $.Autocomplete).toBe(true);
387+
});
376388
});

src/jquery.autocomplete.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@
600600

601601
return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value;
602602
},
603-
604-
dispose: function() {
603+
604+
dispose: function () {
605605
var that = this;
606606
that.el.off('.autocomplete').removeData('autocomplete');
607607
that.disableKillerFn();
@@ -611,9 +611,15 @@
611611

612612
// Create chainable jQuery plugin:
613613
$.fn.autocomplete = function (options, args) {
614+
var dataKey = 'autocomplete';
615+
// If function invoked without argument return
616+
// instance of the first matched element:
617+
if (arguments.length === 0) {
618+
return this.first().data(dataKey);
619+
}
620+
614621
return this.each(function () {
615-
var dataKey = 'autocomplete',
616-
inputElement = $(this),
622+
var inputElement = $(this),
617623
instance = inputElement.data(dataKey);
618624

619625
if (typeof options === 'string') {

0 commit comments

Comments
 (0)