1

¿Cómo puedo hacer para que cuando de 3 veces en Añadir, el boton se desactive?

 <button id='botones' type='submit' name='enviar'>Añadir</button>
            <script>
            var cantidad=document.getElementById("botones")
            cantidad.addEventListener("onclick",()=>{
              if(cantidad.value>3){
                document.getElementById("botones").disabled=true
              }
            })
            </script>

1 respuesta 1

2
  • El evento que estas escuchando es incorrecto deberías estar escuchando el evento click.
    • Para controlar la cantidad de veces que se le da click al botón es necesario crear una variable externa para llevar el contador de clicks.

Quedaria algo asi.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible"
          content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id='botones'
            type='submit'
            name='enviar'>Añadir</button>
    <script>
        var boton = document.getElementById("botones")
        var cantidad = 0;
        boton.addEventListener("click", () => {
            if (cantidad >= 2) {
                boton.disabled = true
            }
            cantidad = cantidad + 1;
        })
    </script>
</body>

</html>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.