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

135
Vistas
Make Whole Div Clickable in React

I have checkbox and its working well. However I wanted the whole div to be clickable also not just the checkbox alone.

Pls check here CLICK HERE

<form onSubmit={handleSubmit}>
      {products.map((product) => (
        <div key={product} style={{ cursor: "pointer" }}>
          {product}
          <Checkbox
            name="products"
            value={product}
            checked={values.products.includes(product)}
            onChange={({ target }) => {
              let updatedProducts;
              if (values.products.includes(product)) {
                updatedProducts = values.products.filter(
                  (product) => product !== target.value
                );
              } else {
                updatedProducts = [...values.products, target.value];
              }
              setFieldValue("products", updatedProducts);
            }}
          />
        </div>
      ))}
      <button type="submit">Submit</button>
    </form>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Wrap the checkbox in label element. This will make the text, i.e. "label" clickable as part of the input. Since label elements are display: inline you will need to also override the display CSS to maintain it as a block level element.

<form onSubmit={handleSubmit}>
  {products.map((product) => (
    <label key={product} style={{ cursor: "pointer", display: "block" }}>
      {product}
      <Checkbox
        name="products"
        value={product}
        checked={values.products.includes(product)}
        onChange={({ target }) => {
          let updatedProducts;
          if (values.products.includes(product)) {
            updatedProducts = values.products.filter(
              (product) => product !== target.value
            );
          } else {
            updatedProducts = [...values.products, target.value];
          }
          setFieldValue("products", updatedProducts);
        }}
      />
    </label>
  ))}
  <button type="submit">Submit</button>
</form>

Edit make-whole-div-clickable-in-react

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to have your product name clickable and change checkbox state, use a label with an htmlFor

You would need to make a few changes to your app like so:

<label for={product}>{product}</label>  <!-- ADD LABEL TAG AND FOR HERE! <!--
<Checkbox
  name="products"
  id={product}  // <-- HAVE YOUR ID SAME WITH THE FOR ABOVE
  value={product}
  checked={values.products.includes(product)}
  onChange={({ target }) => {
    let updatedProducts;
    if (values.products.includes(product)) {
      updatedProducts = values.products.filter(
        (product) => product !== target.value
      );
    } else {
      updatedProducts = [...values.products, target.value];
    }
    setFieldValue("products", updatedProducts);
  }}
/>;

And inside Input component:

<Input
  type="checkbox"
  id={id}  // <-- CHANGE ID TO USE ACTUAL ID ABOVE
  name={name}
  value={value}
  checked={checked}
  onChange={onChange}
/>;

Edit checkbox-styled-components (forked)

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