I m trying to create 2 select boxes in which options of 1st select box are fixed but the second ones change based on the selected value of first div. eg: 1st Select: Solution 1:
You can create a lookup table object that holds both integers and Alphabets with each has a relevant key.
Then in one select you update the state with selected key and in the other select you render the options correspond to the selected key.
Here is a running example:
const lookup = {
"int": [
{ id: '1', text: '1' },
{ id: '2', text: '2' },
{ id: '3', text: '3' },
{ id: '4', text: '4' },
{ id: '5', text: '5' }
],
"abc": [
{ id: 'a', text: 'a' },
{ id: 'b', text: 'b' },
{ id: 'c', text: 'c' },
{ id: 'd', text: 'd' },
{ id: 'e', text: 'e' }
]
}
classAppextendsReact.Component {
constructor(props) {
super(props);
this.state = {
dataValue: 'int'
}
}
onChange = ({ target: { value } }) => {
this.setState({ dataValue: value });
}
render() {
const { dataValue } = this.state;
const options = lookup[dataValue];
return (
<div><selectonChange={this.onChange}><optionvalue="int">Integers</option><optionvalue="abc">Alphabets</option></select><hr /><select>
{options.map(o => <optionkey={o.id}value={o.id}>{o.text}</option>)}
</select></div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));Copy<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><divid="root"></div>CopySolution 2:
Hope this is helpful <!DOCTYPE html><html><body><selectid="sel"onchange="ChangeList()"><optionvalue="">select</option><optionvalue="I">Integer</option><optionvalue="A">Alphabet</option></select><selectid="type"></select><script>varIntandAlph = {};
IntandAlph['I'] = ['1', '2', '3','4','5','6','7','8','9','10'];
IntandAlph['A'] = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
functionChangeList() {
var selList = document.getElementById("sel");
var typeList = document.getElementById("type");
var seltype = selList.options[selList.selectedIndex].value;
while (typeList.options.length) {
typeList.remove(0);
}
varNum = IntandAlph[seltype];
if (Num) {
var i;
for (i = 0; i < Num.length; i++) {
var sel = newOption(Num[i], i);
typeList.options.add(sel);
}
}
}
</script></body></html>Copy
Share
Post a Comment
for "Load Options Of A Select Box Dynamically Based On Value Of Another Select Box In React"
Top Question
Input Type Datetime-local Is Not Working In Firefox
I have an input type of datetime-local which is working fin…
Google Org Chart Data Overflow Outside Of Div
I'm using Google Org chart for a project and the conten…
Using Bootstrap 4 Flexbox To Equally Divide Four Charts
I am trying to plot four different stacked charts in one br…
CSS Lighten Child Elements On Parent Mouseover
here is my problem. I have a div which contains two other d…
Div Is Shown In Wrong Position
I have such code: Solution 1: Add margin: 0 auto …
Some Weird Thing With Clear Both Via Css Pseudo :after
Examine this code: HTML Sometext over here CSS .one { di…
Disable Drop Down Option Using Jquery
I need to disable options with value '- Sold Out -'…
Element On Top Of Parent's Sibling
I am trying to get an element to appear on top of its paren…
Div Centering CSS
I am struggling with my website, I am trying to get the div…
Flex-box Only Display As Many Items That Fit In A Container
I want to only use CSS to make it so I have a container whi…
December 2024
(1)
November 2024
(37)
October 2024
(69)
September 2024
(20)
August 2024
(358)
July 2024
(338)
June 2024
(679)
May 2024
(1294)
April 2024
(687)
March 2024
(758)
February 2024
(840)
January 2024
(759)
December 2023
(809)
November 2023
(367)
October 2023
(556)
September 2023
(291)
August 2023
(325)
July 2023
(276)
June 2023
(346)
May 2023
(212)
April 2023
(139)
March 2023
(143)
February 2023
(174)
January 2023
(255)
December 2022
(121)
November 2022
(228)
October 2022
(809)
September 2022
(164)
August 2022
(289)
July 2022
(93)
Menu Halaman Statis
Beranda
© 2022 - Html5 Tutorial