I have a dummy html code where I’m testing the JS code.
This code has three textboxes:
dynamic
static
both
For each of these I want to show different auto-complete options:
var dynamic = ["JavaScript", "Python", "Ruby"]
var static = ["C", "C++", "Java"]
var both = ["JavaScript", "Python", "Ruby", "C", "C++", "Java"]
What I'm currently doing is to call a function, which has the ID and options as parameters, three times, once for each case:
function show_options(field_id, available_options) {
$(`[id*=${field_id}]`).autocomplete({
source: available_options,
minLength: 0
})
.focus(function() {
$(this).autocomplete("search", $(this).val())
});
};
var dynamic = ["JavaScript", "Python", "Ruby"]
var static = ["C", "C++", "Java"]
var both = ["JavaScript", "Python", "Ruby", "C", "C++", "Java"]
show_options("dynamic", dynamic)
show_options("static", static)
show_options("both", both)
<div class="ui-widget">
<label for=“tags”>Input1: </label>
<input id="dynamic">
</div>
<div class="ui-widget">
<label for="tags">Input2: </label>
<input id="static">
</div>
<div class="ui-widget">
<label for="input3">Input3: </label>
<input id="both">
</div>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet” href=“/resources/demos/style.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>
This kind of works, but it does look as a very bad practice to me.
The reason I'm doing it this way is because I will not know the IDs beforehand, and the options will be received from and external server.