I have select box and input field, if input field value equal substring first 5 character in option value so set this option as a selected option.
I tired the following:
$(document).ready(function() {
$('#input').change(function() {
var inputVal = $(this);
$('#select-box1 option').each(function() {
var sb1_option = $(this).substring(0, 5);
if (inputVal == sb1_option) {
//What should I write here
} else {
//What should I write here
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select-box1">
<option value="00001-test 1">00001-test 1</option>
<option value="00002-test 2">00002-test 2</option>
<option value="00003-test 3">00003-test 2</option>
<option value="00004-test 4">00004-test 4</option>
<option value="00005-test 5">00005-test 5</option>
</select>
<input id="input" type="text" name="Input" value="">