Seleccionar un grupo de chekbox’s con javascript

El siguiente script muestra varios grupos de checkbox’s que se pueden seleccionar/deseleccionar pulsando un checkbox padre.

Tenemos el siguiente <div> que muestra los checkbox’s, tanto el padre como sus hijos:

<div style="border:solid 1px black; margin:4px;">
    <p> <input type="checkbox" onchange="cambiaGrupo(this)" />    TODOS / NINGUNO</p>
    <p> <input type="checkbox" /> chk1</p>
    <p> <input type="checkbox" /> chk2</p>
    <p> <input type="checkbox" /> chk3</p>
    <p> <input type="checkbox" /> chk4</p>
</div>

Si nos fijamos en el código, vemos que en el input padre llamamos a una función al cambiar su valor. Esta función es la siguiente:

<script>

function cambiaGrupo(chk) {
    var padreDIV=chk;
    while( padreDIV.nodeType==1 && padreDIV.tagName.toUpperCase()!="DIV" )
        padreDIV=padreDIV.parentNode;
    //ahora que padreDIV es el DIV, cogeremos todos sus checkboxes
    var padreDIVinputs=padreDIV.getElementsByTagName("input");
    for(var i=0; i<padreDIVinputs.length; i++) {
        if( padreDIVinputs[i].getAttribute("type")=="checkbox" )
            padreDIVinputs[i].checked = chk.checked;
    }
}

</script>

La función se encarga de seleccionar o deseleccionar el grupo de checkboxes del padre.

Ver ejemplo en funcionamiento

Autor
Escrito por Jose Aguilar - Director ejecutivo y tecnológico en JA Modules. Experto programador PrestaShop y Experto programador WordPress.
Te ha servido? Valora esta entrada!
(1 voto, promedio: 5 de 5)
Comparte en las redes sociales
¿Buscas trabajo de programador?

9 respuestas a “Seleccionar un grupo de chekbox’s con javascript”

  1. leadership skill dice:

    An interesting discussion is worth comment. There’s no doubt that that you ought to write more about this topic, it may not be a taboo matter but generally people do not talk about these topics. To the next! Kind regards!!

  2. climatiseur mobile dice:

    You are a very smart person!

  3. ceramic watches dice:

    That is very fascinating, You are an excessively professional blogger. I have joined your feed and look forward to in the hunt for more of your wonderful post. Additionally, I have shared your web site in my social networks!

  4. chaussure de foot dice:

    I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again!

  5. Filmy torrenty dice:

    Howdy! I’m at work surfing around your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the outstanding work!

  6. diet plan dice:

    As I website owner I believe the written content here is real fantastic , appreciate it for your efforts.

  7. I know this site provides quality based posts and extra information, is there any other web site which presents such things in quality?

  8. agen bola sbobet dice:

    Great post, I think blog owners should larn a lot from this weblog its real user genial . «You don’t have to deserve your mother’s love. You have to deserve your father’s.» by Robert Frost.

  9. Tynisha Fallin dice:

    I got what you mean , regards for posting .Woh I am happy to find this website through google. «If one does not know to which port one is sailing, no wind is favorable.» by Seneca.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.

Ver más sobre