Reputation:
i have here a code from http://jqueryui.com/autocomplete/ it works really good but i cant find a way to get the value of selected item in the text view i tried something like this but its not working
<script>
$(document).ready(function () {
$('#tags').change(function () {
$('#tagsname').html('You selected: ' + this.value);
}).change();
});
</script>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
<script>
$(document).ready(function () {
$('#tags').change(function () {
$('#tagsname').html('You selected: ' + this.value);
}).change();
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
<div id="tagsname"></div>
</div>
</body>
</html>
Upvotes: 20
Views: 122346
Reputation: 31
Okay, so in my particular case I had multiple text inputs which used the same class, all of which utilized jQuery Autocomplete with the same autocomplete data set.
Since all these inputs would require their values to be set in order to be successfully submitted via $_POST, I was baffled as to why the selected value would not be set for the value attribute of each input after selecting a value from the predefined autocomplete list.
After a lot of back and forth, and since all mentioned inputs shared the same core class, this is what I ended up doing:
// setup autcomplete
$('.sample-input').autocomplete({
// define source data
source: autocomp_data,
// setup input event as per docs
select: function(event, ui) {
// set the target element
let target = $(this);
// retrieve selected value from event
let selected = ui.item.value;
// set target value using .attr (using .val() does not work, for some reason)
target.attr('value', selected);
}
});
In this way I was actually able to set the correct value for each input and get submission via $_POST to work. Not sure why jQuery UI Autocomplete does not do this by default (I'm sure some guru on SE knows), but yes, the above snippet solved the problem for me.
Upvotes: 0
Reputation: 2118
$(document).ready(function () {
$('#tags').on('change', function () {
$('#tagsname').html('You selected: ' + this.value);
}).change();
$('#tags').on('blur', function (e, ui) {
$('#tagsname').html('You selected: ' + ui.item.value);
});
});
Upvotes: 1
Reputation: 36954
To answer the question more generally, the answer is:
select: function( event , ui ) {
alert( "You selected: " + ui.item.label );
}
Complete example :
$('#test').each(function(i, el) {
var that = $(el);
that.autocomplete({
source: ['apple','banana','orange'],
select: function( event , ui ) {
alert( "You selected: " + ui.item.label );
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
Type a fruit here: <input type="text" id="test" />
Upvotes: 23
Reputation: 37947
I wanted something pretty close to this - the moment a user picks an item, even by just hitting the arrow keys to one (focus), I want that data item attached to the tag in question. When they type again without picking another item, I want that data cleared.
(function() {
var lastText = '';
$('#MyTextBox'), {
source: MyData
})
.on('autocompleteselect autocompletefocus', function(ev, ui) {
lastText = ui.item.label;
jqTag.data('autocomplete-item', ui.item);
})
.keyup(function(ev) {
if (lastText != jqTag.val()) {
// Clear when they stop typing
jqTag.data('autocomplete-item', null);
// Pass the event on as autocompleteclear so callers can listen for select/clear
var clearEv = $.extend({}, ev, { type: 'autocompleteclear' });
return jqTag.trigger(clearEv);
});
})();
With this in place, 'autocompleteselect' and 'autocompletefocus' still fire right when you expect, but the full data item that was selected is always available right on the tag as a result. 'autocompleteclear' now fires when that selection is cleared, generally by typing something else.
Upvotes: 0
Reputation: 388316
When autocomplete changes a value, it fires a autocompletechange event, not the change event
$(document).ready(function () {
$('#tags').on('autocompletechange change', function () {
$('#tagsname').html('You selected: ' + this.value);
}).change();
});
Demo: Fiddle
Another solution is to use select event, because the change event is triggered only when the input is blurred
$(document).ready(function () {
$('#tags').on('change', function () {
$('#tagsname').html('You selected: ' + this.value);
}).change();
$('#tags').on('autocompleteselect', function (e, ui) {
$('#tagsname').html('You selected: ' + ui.item.value);
});
});
Demo: Fiddle
Upvotes: 43