Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How to get the values from <select multpile={true}> in ReactJs

I have a form that has a select-multiple field. It is just getting one value that is first clicked but not the other values that are clicked after clicking the first one. Hhow to get the values? Below is my code.

assignment of state, Here shift and board are the state to store the values of multi-select field

 const [formData, setFormData] = useState({
    name: "",
    photo: "",
    adhar: "",
    phone: "",
    location: "",
    fullAddress: "",
    age: "",
    gender: 0,
    schooName: "",
    experience: "",
    teachForNoDays: "",
    shift: [],
    subject: "",
    teachClass: "",
    board: [],
    homeTuition: false,
    fees: "",
    homeTuitionFees: "",
  });

onChange function

const changeFormData = (e) => {
    // this is how we do when to specifically define a type in the target
    const { name, type } = e.target;
    console.log(e.target.value);
    setFormData({
      ...formData,
      [name]: e.target[type === "checkbox" ? "checked" : "value"],
    });
  };

the fields, only putting up the shift one here

<select
   name="shift"
   id="shift"
   defaultValue={shift}
   size={2}
  onChange={(e) => changeFormData(e)}
   multiple={true}
  >
     <option value="Evening">Evening</option>
     <option value="Morning">Morning</option>
</select>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

<select> has an e.target.options field which is an array containing all the selected options.

You should take that into consideration in changeFormData().

Don't forget to properly "control" the value of your select component by assigning it with the current value:

<select
   ...
   value={formData.shift}
   multiple
  >
     ...
</select>
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda