Skip to content

Commit

Permalink
Merge pull request #13 from dsanders11/align_right
Browse files Browse the repository at this point in the history
Add option for aligning autocomplete with right edge of input
  • Loading branch information
jpic committed Dec 20, 2015
2 parents 8f3ce69 + e9a7624 commit e1536e4
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/autocomplete.js
Expand Up @@ -204,6 +204,12 @@ yourlabs.Autocomplete = function (input) {
*/
this.hideAfter = 200;

/*
Normally the autocomplete box aligns with the left edge of the input. To
align with the right edge of the input instead, change this variable.
*/
this.alignRight = false;

/*
The server should have a URL that takes the input value, and responds
with the list of choices as HTML. In most cases, an absolute URL is
Expand Down Expand Up @@ -486,6 +492,7 @@ yourlabs.Autocomplete.prototype.show = function(html) {
// body as argument.
if (html !== undefined) {
this.box.html(html);
this.fixPosition();
}

// Don't display empty boxes.
Expand All @@ -505,6 +512,7 @@ yourlabs.Autocomplete.prototype.show = function(html) {
// Show the inner and outer container only if necessary.
if (!this.box.is(':visible')) {
this.box.css('display', 'block');
this.fixPosition();
}
};

Expand Down Expand Up @@ -610,6 +618,10 @@ yourlabs.Autocomplete.prototype.fixPosition = function() {
left = (inputLeft - parentLeft) + 'px';
}

if (this.alignRight) {
left = (findPosX(el) + el.scrollLeft - (this.box.outerWidth() - this.input.outerWidth())) + 'px';
}

this.box.appendTo(this.container).css({
position: 'absolute',
minWidth: parseInt(this.input.outerWidth()),
Expand Down

0 comments on commit e1536e4

Please sign in to comment.