Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
How to use check box in forms and control the input fields

I am having a form and I am getting data from solutestate and the output is sent to the first input box using value={solutestate} but I want to toggle the output using the checkbox in the input box

Goal: when I check the check box the solutestate value should be in the first input box and if unchecked the output of the solutestate should be in second input box.

const [state, setState] = useState(false);
const [solutestate, setSoluteState] = useState();
  <input
      className="mr-2 leading-tight"
      type="checkbox"
      onChange={setState}
  />
  <form
      noValidate
      onSubmit={handleSubmit(onSubmit)}
      className="space-x-4"
  >
      <input
         className="shadow"
         {...register("solute")}
         placeholder="SOLUTE"
         onChange={(e) => setSoluteState(e.target.value)}
         value={solutestate}
      />
      <input
         className="shadow"
         {...register("solvent")}
         placeholder="SOLVENT"
      />
  </form>

enter image description here enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try below example. initially since checkbox is unchecked value will be in second input.

const [solutestate, setSoluteState] = useState("");
const [input1, setInput1] = useState("");
const [input2, setInput2] = useState("");
const [syncToFirst, setSyncToFirst] = useState();
<input
  className="mr-2 leading-tight"
  type="checkbox"
  onChange={(event)=>{syncToFirst(event.target.checked?1:2)}}
/>
<form
  noValidate
  onSubmit={handleSubmit(onSubmit)}
  className="space-x-4"
>
  <input
     className="shadow"
     {...register("solute")}
     placeholder="SOLUTE"
     onChange={(e) => syncToFirst === 1 ? setSoluteState(e.target.value) : setInput1(e.target.value)}
     value={syncToFirst === 1 ? solutestate : input1}
  />
  <input
     className="shadow"
     {...register("solvent")}
     placeholder="SOLVENT"
     onChange={(e) => syncToFirst === 2 ? setSoluteState(e.target.value) :input2(e.target.value)}
     value={syncToFirst === 2 ? solutestate : input2}
  />
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try the code below. This should work
    const [solutestate, setSoluteState] = useState("");
    const [input2, setInput2] = useState("");
    <input
      className="mr-2 leading-tight"
      type="checkbox"
      onChange={(event)=>{event.target.checked && setInput2(solutestate)}}
    />
    <form
      noValidate
      onSubmit={handleSubmit(onSubmit)}
      className="space-x-4"
    >
      <input
         className="shadow"
         {...register("solute")}
         placeholder="SOLUTE"
         onChange={(e) => setSoluteState(e.target.value)}
         value={solutestate}
      />
      <input
         className="shadow"
         {...register("solvent")}
         placeholder="SOLVENT"
         onChange={(e) => input2(e.target.value)}
         value={input2}
      />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda