Skip to content

Commit 74e0d4f

Browse files
committed
Autocomplete: Added appendTo option. Fixes #5836 - Autocomplete: add appendTo option.
1 parent 245b932 commit 74e0d4f

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

tests/unit/autocomplete/autocomplete.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ <h2 id="qunit-userAgent"></h2>
3636

3737
<div id="main" style="position: absolute; top: -10000px; left: -10000px;">
3838

39-
<div><input id="autocomplete" class="foo" /></div>
40-
39+
<div id="ac-wrap1" class="ac-wrap"></div>
40+
<div id="ac-wrap2" class="ac-wrap"><input id="autocomplete" class="foo" /></div>
4141
</div>
4242

4343
</body>

tests/unit/autocomplete/autocomplete_defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
var autocomplete_defaults = {
6+
appendTo: "body",
67
delay: 300,
78
disabled: false,
89
minLength: 1,

tests/unit/autocomplete/autocomplete_options.js

+30
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,36 @@ test("cache: false", function() {
6868

6969
var data = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"];
7070

71+
test( "appendTo", function() {
72+
var ac = $( "#autocomplete" ).autocomplete();
73+
same( ac.autocomplete( "widget" ).parent()[0], document.body, "defaults to body" );
74+
ac.autocomplete( "destroy" );
75+
76+
ac.autocomplete({
77+
appendTo: "#ac-wrap2"
78+
});
79+
same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap2" )[0], "id" );
80+
ac.autocomplete( "destroy" );
81+
82+
ac.autocomplete({
83+
appendTo: ".ac-wrap"
84+
});
85+
same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "class" );
86+
same( $( "#ac-wrap2 .ui-autocomplete").length, 0, "class - only appends to one element")
87+
ac.autocomplete( "destroy" );
88+
89+
ac.autocomplete({
90+
appendTo: null
91+
});
92+
same( ac.autocomplete( "widget" ).parent()[0], document.body, "null" );
93+
ac.autocomplete( "destroy" );
94+
95+
ac.autocomplete().autocomplete( "option", "appendTo", "#ac-wrap1" );
96+
same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "modified after init" );
97+
ac.autocomplete( "destroy" );
98+
});
99+
100+
71101
test("delay", function() {
72102
var ac = $("#autocomplete").autocomplete({
73103
source: data,

ui/jquery.ui.autocomplete.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
$.widget( "ui.autocomplete", {
1818
options: {
19+
appendTo: "body",
1920
delay: 300,
2021
minLength: 1,
2122
position: {
@@ -104,7 +105,7 @@ $.widget( "ui.autocomplete", {
104105
};
105106
this.menu = $( "<ul></ul>" )
106107
.addClass( "ui-autocomplete" )
107-
.appendTo( "body", doc )
108+
.appendTo( $( this.options.appendTo || "body", doc )[0] )
108109
// prevent the close-on-blur in case of a "slow" click on the menu (long mousedown)
109110
.mousedown(function() {
110111
// use another timeout to make sure the blur-event-handler on the input was already triggered
@@ -166,11 +167,14 @@ $.widget( "ui.autocomplete", {
166167
$.Widget.prototype.destroy.call( this );
167168
},
168169

169-
_setOption: function( key ) {
170+
_setOption: function( key, value ) {
170171
$.Widget.prototype._setOption.apply( this, arguments );
171172
if ( key === "source" ) {
172173
this._initSource();
173174
}
175+
if ( key === "appendTo" ) {
176+
this.menu.element.appendTo( $( value || "body", this.element.ownerDocument )[0] )
177+
}
174178
},
175179

176180
_initSource: function() {

0 commit comments

Comments
 (0)