Last active
June 4, 2024 17:27
-
-
Save JuanjoSalvador/0d9f93df0f4990e3c660 to your computer and use it in GitHub Desktop.
Contador de checkboxes en JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function contar() { | |
var checkboxes = document.getElementById("test").checkbox; // Array de checkboxes | |
var cont = 0; // Checkboxes marcados, por defecto empieza la cuenta en 0 | |
for (var i = 0; i < checkboxes.length; i++) { | |
if (checkboxes[i].checked) { | |
cont = cont++; | |
} | |
} | |
var porcen = (cont/checkboxes.length) * 100; // Porcentaje de checboxes marcados | |
if (cont == checkboxes.length) { | |
alert("¡Todos marcados!"); | |
} else { | |
alert("Resultado: " + cont + "/" + checkboxes.length + ". Porcentaje de checkboxes marcados: " + porcen); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Al editor de Github Gist se la suda un poco el formato que yo le de a mi código con los sangrados.