Using select multiple with react

React JS

Here is an example


const [myList, setMyList] = useState([]);

function updateList(e, setState) { 
   let list = [];
   for (var i = 0, l = e.target.options.length; i < l; i++) {
        if (e.target.options[i].selected) {
            list.push(e.target.options[i].value);
        }
   }

   setState(list);
}

 <select className="form-select" multiple onChange={(e) => updateList(e, setMyList)}
   value={myList}>
   
   <option value="a">A</option>
   <option value="b">B</option>
   <option value="c">C</option>
   <option value="d">D</option>
 </select>

Post a Comment

Previous Post Next Post