With only a rudimentary knowledge of JQuery and Javascript, this is killing me. Essentially I want to have a drop-down list autoselect when certain text is entered in the input box. eg. "Foo" is a "Category4" item, so the select box should choose option 19 automatically if someone types Foo in the input box. "Bar" is a "Category7" item, so the select box should choose option 8 is someone types Bar in the input box.
The reason this isn't hardcoded is because the user will still have the option to recategorize these items if they see fit. ie. Normally "Bar" is a "Category7" item, so the script should automatically select option 8, but the user can change "Bar" to be a "Category1" item by manually selecting it in the dropdown box, and the script shouldn't overwrite the manual selection.
I've scrounged the script below that lets me type in the name of the Category in the input box and have it populate the dropdown, but I can't get to the next step of taking the input, comparing it to a list of predefined responses and using that to populate the dropdown.
<p id="demo"></p>
<input type="text" id="input" placeholder="input">
<select id="category">
<option value="categoryplaceholder">Category</option>
<option value="5">Category1</option>
<option value="22">Category2</option>
<option value="3">Category3</option>
<option value="19">Category4</option>
<option value="23">Category5</option>
<option value="25">Category6</option>
<option value="8">Category7</option>
</select>
<input type="submit" value="submit">
$(document).ready(function() {
$("#input").change(function() {
var val = $("#input").val();
$("#category > option").each(function() { //Run through the loop of each option
if (this.text.indexOf(val) >= 0) { //Find if the string present as substring
$("#category > option").removeAttr("selected"); //Remove the existing selected option
$(this).attr("selected", "selected"); //Select this matching option as selected
return false; //Return after first match is found
}
});
});
});
Super grateful for any help! https://jsfiddle.net/chuckler/w52kfpcy/