So I have a select field called TRIP ID whose options come from a database. I also have another select field called APS Weight which has three options. STA, STB, STC – but the values of these 3 options is actually numbers not STA, STB, STC. What STA, STB and STC is, is actually the InnerHTML of these select options.
My issue is that i want to select a TRIP ID right, then onChange the selected option of APS Weight becomes the first three letters of the value of TRIP ID.
<select name="tripid" id="tripid" style="width: 300px"
onchange="document.getElementById('aps').value=tripid.value.slice(0,3)">
I think my code isnt working because. The value of the options on APS Weight is actually numbers and not strings.
3
Answers
You need to match the value of the selected option in the
$('#tripid')
dropdown with the text content of the options in the$('#aps')
dropdown.Here are two solutions (JQuery using
option:contains
and pure Javascript usingoption.text
).JQuery:
Pure Javascript:
If you can add a data attribute to the options, you can refer the same in the onChange method.