diff --git a/auto-complete.js b/auto-complete.js index 2f5a88c..df33bc7 100644 --- a/auto-complete.js +++ b/auto-complete.js @@ -37,6 +37,7 @@ var autoComplete = (function(){ offsetTop: 1, cache: 1, menuClass: '', + selectorToInsert: 0, renderItem: function (item, search){ // escape special characters search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); @@ -82,7 +83,12 @@ var autoComplete = (function(){ } } addEvent(window, 'resize', that.updateSC); - document.body.appendChild(that.sc); + + if (typeof o.selectorToInsert === "string" && document.querySelector(o.selectorToInsert) instanceof HTMLElement) { + document.querySelector(o.selectorToInsert).appendChild(that.sc); + } else { + document.body.appendChild(that.sc); + } live('autocomplete-suggestion', 'mouseleave', function(e){ var sel = that.sc.querySelector('.autocomplete-suggestion.selected'); @@ -204,7 +210,16 @@ var autoComplete = (function(){ that.setAttribute('autocomplete', that.autocompleteAttr); else that.removeAttribute('autocomplete'); - document.body.removeChild(that.sc); + try { + if (o.selectorToInsert && document.querySelector(o.selectorToInsert).contains(that.sc)) { + document.querySelector(o.selectorToInsert).removeChild(that.sc); + } else { + document.body.removeChild(that.sc); + } + } catch (error) { + console.log('Destroying error: can\'t find target selector', error); + throw error; + } that = null; } }; diff --git a/testcase.html b/testcase.html new file mode 100644 index 0000000..effab0e --- /dev/null +++ b/testcase.html @@ -0,0 +1,39 @@ + + + + + + + Document + + + +
+ +
+ + + + + + \ No newline at end of file