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

127
Vistas
How to restrict the user from checking a specific checkbox together?

<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label><br>

let's say we have this code can i disallow the user to choose bike and boat together using javascript or anything else

i have tried this javascript code to limit the choices

$(document).ready(function () { $("input[name='value3']").change(function () { var maxAllowed = 2; var cnt = $("input[name='value3']:checked").length; if (cnt > maxAllowed) { $(this).prop("checked", ""); alert('You can select maximum ' + maxAllowed + ' options!!'); } }); });

but i still need to limitate the choices more and prevent choosing a specific option along with a specific one

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

0

You can unselect all other checkboxes when the user selects a checkbox by listening for the change event:

const checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(e => e.addEventListener('change', () => checkboxes.forEach(f => f != e ? f.checked = false : '')))
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label><br>


However, the better choice is to make the inputs radio buttons instead of checkboxes.

<input type="radio" id="vehicle1" name="vehicle" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="radio" id="vehicle2" name="vehicle" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="radio" id="vehicle3" name="vehicle" value="Boat">
<label for="vehicle3"> I have a boat</label><br>

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