$(document).on('click', 'button.setValueClick', function(){
//obtain <input> element that should be changed
var ele = $('#' + $(this).attr('ele'));
//call setValue from the autocomplete widget,
//passing the value from the button's value attribute
ele.autocomplete('setValue', $(this).attr('value'));
});
$(document).on('click', 'button.getValueClick', function(){
//obtain <input> element that should be changed
var ele = $('#' + $(this).attr('ele'));
//alert the returned value from the autocomplete getValue method
//note that you could do anything with this, instead of just alert
alert(ele.autocomplete('getValue'));
});
$(function() {
var availableTags = [
{ id: 1, label: 'one' },
{ id: 2, label: 'two' },
{ id: 3, label: 'three' },
{ id: 23, label: 'two-thirds' }
];
$.widget("ui.autocomplete", $.ui.autocomplete, {
setValue: function(id) {
//get the actual input field instead of the jQuery autocomplete object
var input = $(this.element[0]);
//loop through availableTags,
//v will be an object within the array
var tags = $(this.element[0]).autocomplete( "option", "source" );
$.each(tags, function(k, v){
//if the id property of the object matches the id passed to setValue,
//set the input's value to the label property of the object
if(v.id == id){
input.val(v.label);
//if found, break the loop to save precious resources
return false;
}
});
},
getValue: function(){
//get the actual input field instead of the jQuery autocomplete object
var val = $(this.element[0]).val();
//this is what will be returned,
//if no corresponding availableTag object matches,
//false will be...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.