The sentence structure of my autocomplete is:
<Occupation> <for> <time period>
Example:
In the database, I have a column containing a list of occupations. With jQuery UI autocomplete, I populate the first two sections in the text input using the following codes:
<script>
$(function() {
$("#occupation").autocomplete({
source: 'search.php',
delay: 500,
select: function(event, ui) {
$(this).val(ui.item.value)
}
});
});
</script>
<input type='text' name='occupation' id='occupation’>
The search.php
code:
<?php
$searchTerm = $_GET['term'];
$query = $db->query("SELECT DISTINCT occupation FROM table WHERE occupation LIKE '%" . $searchTerm . "%' ORDER BY occupation ASC");
$a_json = array();
while ($row = $query->fetch_assoc())
{
$data_row["value"] = $row['occupation'] . ' for';
$data_row["label"] = $row['occupation'];
array_push($a_json, $data_row);
}
echo json_encode($a_json);
Now, is there any way to create a second trigger to make an autocomplete for the rest? Like after selecting an occupation, if the user inputs 5
, it’ll show the following autocomplete options for the time period: 5 days/ 5 weeks/ 5 months/ 5 years
. Like the image below:
Thanks in advance for your suggestions.
The autocomplete attribute specifies whether a form or an input field should have autocomplete on or off. Autocomplete allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values.
Approach: Create a div with the class as a container. Inside of this div, create another div with a class as a text-container that will contain the <input> tag & <datalist> tag. Declare the list attribute as programmingLanguages inside the <input> tag.
You can try HTML <datalist> tag for autocomplete dropdown.
I would advise following the Multiple example shown here: http://jqueryui.com/autocomplete/#multiple
Basically, we're going to selectively pick out tags to fill in, using " for"
as your delimiter instead of ","
.
var occupations = [{
value: "Assistant for",
label: "Assistant"
}, {
value: "Student for",
label: "Student"
}, {
value: "Teacher for",
label: "Teacher"
}];
var oLength = [
"Days",
"Weeks",
"Months",
"Years"
];
$(function() {
function split(val) {
return val.split(/\sfor/);
}
function extractLast(term) {
return split(term).pop();
}
$("#occupation").on("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
source: function(request, response) {
var term = extractLast(request.term);
var results = [];
if (request.term.indexOf("for") > 0) {
var regE = /^(.*)\sfor\s(\d)\s(.*)$/
if (regE.test(request.term)) {
return false;
}
if (parseInt(term) > 0) {
$.each(oLength, function(k, v) {
results.push(term + " " + v);
});
}
} else {
results = $.ui.autocomplete.filter(
occupations, request.term);
}
response(results);
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
terms[0] = terms[0] + " for";
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join("");
return false;
}
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="occupation" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With