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

119
Visualizações
Handle input and checkbox onchange events

I have a use case, where:

  • When the checkbox element is checked, it should set the value of an input with a predefined one, and if it's unchecked, it should be set with a 0.
  • As well, the input can be edited, so it should allow setting a new value, unchecking the checkbox (since the value at this point is not equal to the predefined one)

This is the image that represents what I'm trying to achieve.

enter image description here

To do that, I'm using onchange events in both html elements as I show:

<IonCheckbox slot="start" color='primary' checked={checked} onIonChange={changeCheckbox} />
<IonInput inputMode="numeric" color='light' value={inputValue} onIonChange={changeInputValue} />

The main problem with this is that when I change the checked value, it triggers the input onChange event and vice versa.

const [checked, setChecked] = useState<boolean>(true);
const [inputValue, setInputValue] = useState<string>(value);

function changeCheckbox(event: any) {
   setChecked(event.detail.checked);
   setInputValue(event.detail.checked ? value : '0');
}

function changeInputValue(event: any) {
   setInputValue(event.target.value);

   if (checked) {
     setChecked(false);
   }
}
about 4 years ago · Juan Pablo Isaza
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