Skip to content

Commit

Permalink
Revert to previously selected value when the field is left with inval…
Browse files Browse the repository at this point in the history
…id input
  • Loading branch information
dontangg committed Dec 12, 2011
1 parent 783fabb commit b7f6602
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions jquery.select-to-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ THE SOFTWARE.
'relevancy-sorting-strict-match-value': 5,
'relevancy-sorting-booster-attr': 'data-relevancy-booster',
handle_invalid_input: function( context ) {
context.$text_field.val( '' );
context.$text_field.val( context.$select_field.find('option:selected:first').text() );
},
handle_select_field: function( $select_field ) {
return $select_field.hide();
},
insert_text_field: function( $select_field ) {
insert_text_field: function( context ) {
var $text_field = $( "<input></input>" );
if ( settings['copy-attributes-to-text-field'] ) {
var attrs = {};
var raw_attrs = $select_field[0].attributes;
var raw_attrs = context.$select_field[0].attributes;
for (var i=0; i < raw_attrs.length; i++) {
var key = raw_attrs[i].nodeName;
var value = raw_attrs[i].nodeValue;
Expand All @@ -57,8 +57,14 @@ THE SOFTWARE.
};
$text_field.attr( attrs );
}
return $text_field.val( $select_field.find('option:selected:first').text() )
.insertAfter( $select_field );
$text_field.blur(function() {
var valid_values = context.$select_field.find('option').map(function(i, option) { return $(option).text(); });
if ( !($text_field.val() in valid_values) && typeof settings['handle_invalid_input'] === 'function' ) {

This comment has been minimized.

Copy link
@kuceram

kuceram May 15, 2013

Contributor

@dontangg
The "in" statement actually doesn't work? I found this out when I was using twitter bootstrap autocomplete as an adapter for this. I would use ($.inArray($text_field.val(), valid_values) < 0)

This comment has been minimized.

Copy link
@dontangg

dontangg May 15, 2013

Author Contributor

You are very right! I'm not sure why I thought that was working! I am not a maintainer of this code. I'm sure Jamie would be happy if you submitted a pull request with this fix.

settings['handle_invalid_input'](context);
}
});
return $text_field.val( context.$select_field.find('option:selected:first').text() )
.insertAfter( context.$select_field );
},
extract_options: function( $select_field ) {
var options = [];
Expand Down Expand Up @@ -130,17 +136,17 @@ THE SOFTWARE.

return this.each(function(){
var $select_field = $(this);

var options = settings['extract_options']( $select_field );
var $text_field = settings['insert_text_field']( $select_field );
settings['handle_select_field']( $select_field );


var context = {
'$select_field': $select_field,
'$text_field': $text_field,
'options': options,
'options': settings['extract_options']( $select_field ),
'settings': settings
};

context['$text_field'] = settings['insert_text_field']( context );

settings['handle_select_field']( $select_field );

if ( typeof settings['autocomplete-plugin'] === 'string' ) {
adapters[settings['autocomplete-plugin']]( context );
} else {
Expand Down

0 comments on commit b7f6602

Please sign in to comment.