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

98
Visualizações
onChange event is not working on my Select

Hi i want to do something when mi select box changes, i have something like this:

function Task(){
    const prints = (e) =>{
        console.log("it prints");
    }

    const prints2 = (e) =>{
        console.log("it prints2");
    }

return (
     <select onChange={prints} onClick={prints2} name="subject">
        {subjectsContext.subjects.map((aSubject)=>(
            <option value={aSubject.idsubject}>
                {aSubject.subjectname}
            </option>
        )
        )}
        </select>
)
}

subjects is a list that i have, when i put onClick instead of onChange it works fine, but when i put onChange it dont works when i select a option, it just dont trigger the prints function, why?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to pass the event (e) to the function as a parameter, try this:

function Task() {
  const prints = (e) => {
    console.log(e.target.value);
    console.log('it prints');
  };

  const prints2 = (e) => {
    console.log(e.target.value);
    console.log('it prints2');
  };

  return (
    <select
      onChange={(e) => prints(e)}
      onClick={(e) => prints2(e)}
      name='subject'
    >
      {subjectsContext.subjects.map((aSubject) => (
        <option value={aSubject.idsubject}>{aSubject.subjectname}</option>
      ))}
    </select>
  );
}

Also - consider adding a key attribute to you option as you're mapping trough it and creating duplicate elements in the DOM, I assume this is a react app so it might cause errors.

Edit: ... And you don't need the onClick here, you won't even get an event so simply remove it.

about 4 years ago · Juan Pablo Isaza 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