]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete: When appendTo is a jQuery object or a DOM element, don't search against...
authorScott González <scott.gonzalez@gmail.com>
Wed, 28 Nov 2012 15:33:44 +0000 (10:33 -0500)
committerScott González <scott.gonzalez@gmail.com>
Wed, 28 Nov 2012 15:33:59 +0000 (10:33 -0500)
tests/unit/autocomplete/autocomplete_options.js
ui/jquery.ui.autocomplete.js

index 8331c53d54344c0c53839abf281c557abe778c56..d3a25d2fc8c01818d79a74ed7af93ac6ea8d7b94 100644 (file)
@@ -5,8 +5,9 @@ module( "autocomplete: options" );
 var data = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl" ];
 
 test( "appendTo", function() {
-       expect( 5 );
-       var element = $( "#autocomplete" ).autocomplete();
+       expect( 7 );
+       var detached = $( "<div>" ),
+               element = $( "#autocomplete" ).autocomplete();
        equal( element.autocomplete( "widget" ).parent()[0], document.body, "defaults to body" );
        element.autocomplete( "destroy" );
 
@@ -26,6 +27,18 @@ test( "appendTo", function() {
        element.autocomplete().autocomplete( "option", "appendTo", "#ac-wrap1" );
        equal( element.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "modified after init" );
        element.autocomplete( "destroy" );
+
+       element.autocomplete({
+               appendTo: detached
+       });
+       equal( element.autocomplete( "widget" ).parent()[0], detached[0], "detached jQuery object" );
+       element.autocomplete( "destroy" );
+
+       element.autocomplete({
+               appendTo: detached[0]
+       });
+       equal( element.autocomplete( "widget" ).parent()[0], detached[0], "detached DOM element" );
+       element.autocomplete( "destroy" );
 });
 
 function autoFocusTest( afValue, focusedLength ) {
index f2a7089c4cf975717b6683b5bd42ea8c1f42eac8..5edb84d442655c40b9f383d5021cb8c7060cb097 100644 (file)
@@ -181,7 +181,7 @@ $.widget( "ui.autocomplete", {
                this._initSource();
                this.menu = $( "<ul>" )
                        .addClass( "ui-autocomplete" )
-                       .appendTo( this.document.find( this.options.appendTo || "body" )[ 0 ] )
+                       .appendTo( this._appendTo() )
                        .menu({
                                // custom key handling for now
                                input: $(),
@@ -320,6 +320,14 @@ $.widget( "ui.autocomplete", {
                }
        },
 
+       _appendTo: function() {
+               var element = this.options.appendTo;
+               if ( element && (element.jquery || element.nodeType) ) {
+                       return $( element );
+               }
+               return this.document.find( element || "body" ).eq( 0 );
+       },
+
        _isMultiLine: function() {
                // Textareas are always multi-line
                if ( this.element.is( "textarea" ) ) {