I have two select’s. The values/text of the second one will vary based on what is selected in the first select.
I will have onchange function linked to the first select. But I do not know
- how I can save data for my onchange function in html
- how I can change second select html definition (text/values + number of options) in my javascript code
Note that I don’t want to use any javascript framework
1) I assume you’re seeking advice on how to encode the data for the second select input. I suggest using JavaScript to store a mapping of possible values of the first select to value to be used by the second so you can index it directly, e.g.:
This strategy is nice concise but requires programmatic generation of JavaScript. If you really want to use plain HTML then you could have a nested DIV structure and parse it as needed, e.g.:
2) Now, setting the options of select2 can be done by looking up the data from the value of select1, encoding them as DOM Option objects, and adding them to select1:
Note that this is all untested and has lots of room for improvement but should illustrate the idea.