Created
October 12, 2010 16:45
-
-
Save dabit/622501 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Unobtrusive autocomplete | |
* | |
* To use it, you just have to include the HTML attribute autocomplete | |
* with the autocomplete URL as the value | |
* | |
* Example: | |
* <input type="text" data-autocomplete="/url/to/autocomplete"> | |
* | |
* Optionally, you can use a jQuery selector to specify a field that can | |
* be updated with the element id whenever you find a matching value | |
* | |
* Example: | |
* <input type="text" data-autocomplete="/url/to/autocomplete" id_element="#id_field"> | |
*/ | |
$(document).ready(function(){ | |
$('input[data-autocomplete]').live('focus', function(i){ | |
$(this).autocomplete({ | |
source: $(this).attr('data-autocomplete'), | |
select: function(event, ui) { | |
$(this).val(ui.item.value); | |
if ($(this).attr('id_element')) { | |
$($(this).attr('id_element')).val(ui.item.id); | |
} | |
return false; | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment