»
EnglishFrenchVietnamese

Print - Binary Operators and CheckBox - JavaScriptBank.com

Full version: jsB@nk » Form » Checkbox » Binary Operators and CheckBox
URL: https://www.javascriptbank.com/binary-operators-and-checkbox.html

Binary Operators and CheckBox © JavaScriptBank.comThis JavaScript is a good example of the use of binary operations with check boxes to obtain a single number which is the binary representation of the selected check boxes. There is also a button to show you your selections in text form.

Full version: jsB@nk » Form » Checkbox » Binary Operators and CheckBox
URL: https://www.javascriptbank.com/binary-operators-and-checkbox.html



JavaScript
<script>// Victor Cuervo (mailto:victor@aulambra.com)datos = ['Football','Baloncesto','Atletismo','Balonmano','Gym','Karate'];/* Variable que guarda el valor que se almacenara en la Base de Datos */var checkboxActivados = 0;function activarValor (numero) {checkboxActivados ^= numero;}function potencia ( exponente ) {/* Retorna dos elevando al exponente recibido como parametro */calculo = 1;for (x=0;x<exponente;x++) {calculo = calculo*2;}return calculo;}function listarActivos() {/* Lista los checkbox que hay activados atendiendo a la variable activado. */lista = "Opciones activadas \n";for (x=0; x<datos.length; x++) {activado = checkboxActivados & potencia(x);if (activado != 0) {lista = lista +  " ·" + datos[x] + "\n";}}alert(lista);}function escribirOpciones () {/* Función que crea los checkbox a partir del array */for (x=0; x<datos.length; x++) {document.write("<input type='checkbox' onClick='activarValor(" + potencia(x) + ");'>" + datos[x] + "<br>");}}</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form name="form2">  <p> <script>escribirOpciones();</script>    <input type="button" name="Button1" value="Check the Data" onClick="alert(checkboxActivados);">    <input type="button" name="Button2" value="CheckBox Selections" onClick="listarActivos();">  </p></form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->