cascading dropdown for forms react
JavaScript
import React from 'react';
class Dropdown extends React.Component {
constructor(props) {
super(props);
this.state = {
countries : [],
states : [],
cities : [],
selectedCountry : '--Choose Country--',
selectedState : '--Choose State--'
};
this.changeCountry = this.changeCountry.bind(this);
this.changeState = this.changeState.bind(this);
}
componentDidMount() {
this.setState({
countries : [
{ name: 'Germany', states: [ {name: 'A', cities: ['Duesseldorf', 'Leinfelden-Echterdingen', 'Eschborn']} ] },
{ name: 'Spain', states: [ {name: 'B', cities: ['Barcelona']} ] },
{ name: 'USA', states: [ {name: 'C', cities: ['Downers Grove']} ] },
{ name: 'Mexico', states: [ {name: 'D', cities: ['Puebla']} ] },
{ name: 'India', states: [ {name: 'E', cities: ['Delhi', 'Kolkata', 'Mumbai', 'Bangalore']} ] },
]
});
}
changeCountry(event) {
this.setState({selectedCountry: event.target.value});
this.setState({states : this.state.countries.find(cntry => cntry.name === event.target.value).states});
}
changeState(event) {
this.setState({selectedState: event.target.value});
const stats = this.state.countries.find(cntry => cntry.name === this.state.selectedCountry).states;
this.setState({cities : stats.find(stat => stat.name === event.target.value).cities});
}
render() {
return (
<div id="container">
<h2>Cascading or Dependent Dropdown using React</h2>
<div>
<label>Country</label>
<select placeholder="Country" value={this.state.selectedCountry} onChange={this.changeCountry}>
<option>--Choose Country--</option>
{this.state.countries.map((e, key) => {
return <option key={key}>{e.name}</option>;
})}
</select>
</div>
<div>
<label>State</label>
<select placeholder="State" value={this.state.selectedState} onChange={this.changeState}>
<option>--Choose State--</option>
{this.state.states.map((e, key) => {
return <option key={key}>{e.name}</option>;
})}
</select>
</div>
<div>
<label>City</label>
<select placeholder="City">
<option>--Choose City--</option>
{this.state.cities.map((e, key) => {
return <option key={key}>{e}</option>;
})}
</select>
</div>
</div>
)
}
}
export default Dropdown;
Also in JavaScript:
- Title
- How to copy text from a div to clipboard
- Category
- JavaScript
- Title
- javascript add new element to object
- Category
- JavaScript
- Title
- how to add two attay into object in javascript
- Category
- JavaScript
- Title
- how to freeze js object
- Category
- JavaScript
- Title
- how to set height of material ui dialog react
- Category
- JavaScript
- Title
- angular transition animation
- Category
- JavaScript
- Title
- how to delay redirect in javascript
- Category
- JavaScript
- Title
- button style not working react native
- Category
- JavaScript
- Title
- convert array object to string javascript
- Category
- JavaScript
- Title
- create javascript button
- Category
- JavaScript
- Title
- how to define cardTitle background image in mdl in reactjs
- Category
- JavaScript
- Title
- adding cors in angular
- Category
- JavaScript
- Title
- go to anchor jquery
- Category
- JavaScript
- Title
- getcollectionnames
- Category
- JavaScript
- Title
- color in angular material not working
- Category
- JavaScript
- Title
- change the value in checkbox by button react
- Category
- JavaScript
- Title
- connect nodejs to mariadb
- Category
- JavaScript
- Title
- delete last element of array js
- Category
- JavaScript
- Title
- go to another page using javascript
- Category
- JavaScript
- Title
- how to remove item from array javascript
- Category
- JavaScript
- Title
- getelementbyclass
- Category
- JavaScript
- Title
- body-parser node
- Category
- JavaScript
- Title
- convert class object to json python
- Category
- JavaScript
- Title
- enzyme check state
- Category
- JavaScript
- Title
- javascript array concat spread operator
- Category
- JavaScript
- Title
- at leastone checkbox required jquery
- Category
- JavaScript
- Title
- how to check version of node js in cmd
- Category
- JavaScript
- Title
- .classList
- Category
- JavaScript
- Title
- conditional rendering in react
- Category
- JavaScript
- Title
- disable autocomplete in react fields
- Category
- JavaScript
- Title
- base64 decode javascript
- Category
- JavaScript
- Title
- how to use chart js in vue js
- Category
- JavaScript
- Title
- how to save thing in cookie js
- Category
- JavaScript
- Title
- add table row jQuery
- Category
- JavaScript
- Title
- angular npm angular material
- Category
- JavaScript
- Title
- append array js
- Category
- JavaScript
- Title
- datatable on error.dt
- Category
- JavaScript
- Title
- covid
- Category
- JavaScript
- Title
- declare multiple variables javascript
- Category
- JavaScript
- Title
- how to make javascript function consise
- Category
- JavaScript
- Title
- javascript add scroll listener
- Category
- JavaScript
- Title
- disable javascript chrome
- Category
- JavaScript
- Title
- combine p5 with react
- Category
- JavaScript
- Title
- class javascript
- Category
- JavaScript
- Title
- how to clone an object in javascript
- Category
- JavaScript
- Title
- beautify json python
- Category
- JavaScript
- Title
- how to loop through array of numbers in javascript
- Category
- JavaScript
- Title
- how to add attribute to selected element in javascript
- Category
- JavaScript
- Title
- Alert.alert closing expo
- Category
- JavaScript
- Title
- for of array javascript
- Category
- JavaScript
- Title
- how to add property to object in javascript
- Category
- JavaScript
- Title
- const{}=
- Category
- JavaScript
- Title
- indexing string in javascript
- Category
- JavaScript
- Title
- Array Helpers
- Category
- JavaScript
- Title
- arrow function
- Category
- JavaScript
- Title
- how to load localt ext file in js
- Category
- JavaScript
- Title
- how to access a database in express
- Category
- JavaScript
- Title
- array push javascript example
- Category
- JavaScript
- Title
- append new element for each value in array d3.js
- Category
- JavaScript
- Title
- javascript adding an array to an array
- Category
- JavaScript
- Title
- dropdown list value react fragment
- Category
- JavaScript
- Title
- Javascript check if undefinded
- Category
- JavaScript
- Title
- iterate through nested object javascript
- Category
- JavaScript
- Title
- get value of all checked boxes jquery
- Category
- JavaScript
- Title
- access angular app outside localhost
- Category
- JavaScript
- Title
- Factorial
- Category
- JavaScript
- Title
- how to create a popup in javascript
- Category
- JavaScript
- Title
- how to remove 000webhost watermark 2019
- Category
- JavaScript
- Title
- how to verify timestamp format in javascript
- Category
- JavaScript
- Title
- export aab bundle react native android
- Category
- JavaScript
- Title
- discord js channel count
- Category
- JavaScript
- Title
- arrays javascript
- Category
- JavaScript
- Title
- create node project
- Category
- JavaScript
- Title
- command to start api on react
- Category
- JavaScript
- Title
- firefox extension make ajax request
- Category
- JavaScript
- Title
- how to host react app on heroku
- Category
- JavaScript
- Title
- fsharp newtonsoft json deserialize
- Category
- JavaScript
- Title
- Alternating Sums
- Category
- JavaScript
- Title
- free map js
- Category
- JavaScript
- Title
- comparing arrays javascript
- Category
- JavaScript
- Title
- contact form7 404 wp-json feedback
- Category
- JavaScript
- Title
- how to extract values from array in javascript
- Category
- JavaScript
- Title
- add readonly attribute jquery
- Category
- JavaScript
- Title
- how to get tomorrow date in javascript
- Category
- JavaScript
- Title
- javascript change url hash
- Category
- JavaScript
- Title
- angular 7 folder structure best practices
- Category
- JavaScript
- Title
- how to set three js canvas width 100%
- Category
- JavaScript
- Title
- .shift js
- Category
- JavaScript
- Title
- fix a navbar to top of page js
- Category
- JavaScript
- Title
- chrome extension get current tab from popup
- Category
- JavaScript
- Title
- if statement in ejs
- Category
- JavaScript
- Title
- date to string format javascript
- Category
- JavaScript
- Title
- How do I check if an element is hidden in jQuery?
- Category
- JavaScript
- Title
- click point invert zoom react simple map
- Category
- JavaScript
- Title
- forever loop in js
- Category
- JavaScript
- Title
- how to use jQuery hide and show to display multiple images
- Category
- JavaScript
- Title
- create array with specific size javascript
- Category
- JavaScript
- Title
- angularjs cdn
- Category
- JavaScript
- Title
- how to make a tile generation system in javascript
- Category
- JavaScript
- Title
- how to create simple node project
- Category
- JavaScript