I'm trying to get the value from the selected item of two drop down menu (DDM). The first DDM will populate the second DDM base on user input. E.g. first DDM select "langauge", second DDM will only show "c++, java, python, c#". I'm having difficulty in retrieving the value from both drop down menu as string ? Any suggestions ? Many thanks.
Current Code
/** Function that will set different values to state variable
* based on which dropdown is selected
*/
const changeSelectOptionHandler = (event) => {
setSelected(event.target.value);
};
/** Different arrays for different dropdowns */
const algorithm = [
"Searching Algorithm",
"Sorting Algorithm",
"Graph Algorithm",
];
const language = ["C++", "Java", "Python", "C#"];
const dataStructure = ["Arrays", "LinkedList", "Stack", "Queue"];
/** Type variable to store different array for different dropdown */
let type = null;
/** This will be used to create set of options that user will see */
let options = null;
/** Setting Type variable according to dropdown */
if (selected === "Algorithm") {
type = algorithm;
} else if (selected === "Language") {
type = language;
} else if (selected === "Data Structure") {
type = dataStructure;
}
/** If "Type" is null or undefined then options will be null,
* otherwise it will create a options iterable based on our array
*/
if (type) {
options = type.map((el) => <option key={el}>{el}</option>);
}
return (
<div>
<form>
<div>
{/** Bind changeSelectOptionHandler to onChange method of select.
* This method will trigger every time different
* option is selected.
*/}
<select onChange={changeSelectOptionHandler}>
<option>Choose...</option>
<option>Algorithm</option>
<option>Language</option>
<option>Data Structure</option>
</select>
</div>
<div>
<select>
{
options
}
</select>
</div>
</form>
</div>
);
};
export default App;