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
Can I Use The Id Of An Html Element As A Variable In Javascript?
Accidentally, I noticed I could use the id of an HTML eleme…
How To Change Form Background Color On Focus Loss When Text Is Entered?
First I would like to point out that all of this is client …
Scale A Rectangle In Html And Css
I want to make a rectangle (a div) with a specific scale. T…
How To Align Multiple Input Using Css
i'm trying to realize a simple login page and i need to…
How To Make A Transition Effect Up The Input On Change
I need to place this particular effect on a dropdown I need…
Is Localstorage The Right Choice For This Webapp?
I'm interested in building a small offline webapp and I…
Reducing Table Wrapper Width For Iphone
I am making small jquery app. In it i am using DataTables t…
Jquery Fancybox Overlay Color
I have tried to get a Fancybox iFrame to load automatically…
How To Remove Gap After Footer In Bootstrap 4?
Below is my html code (live demo http://...) but the footer…
Render A React App In A Non React Web Page
I was wondering if its possible to render an entire react a…
December 2024
(1)
November 2024
(40)
October 2024
(62)
September 2024
(18)
August 2024
(359)
July 2024
(331)
June 2024
(681)
May 2024
(1298)
April 2024
(764)
March 2024
(1524)
February 2024
(1689)
January 2024
(1375)
December 2023
(1193)
November 2023
(333)
October 2023
(554)
September 2023
(306)
August 2023
(344)
July 2023
(274)
June 2023
(371)
May 2023
(198)
April 2023
(142)
March 2023
(136)
February 2023
(178)
January 2023
(269)
December 2022
(127)
November 2022
(234)
October 2022
(180)
September 2022
(163)
August 2022
(289)
July 2022
(91)
Menu Halaman Statis
Beranda
© 2022 - Html5 Cafe