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.
Autor
Escrito por Jose Aguilar - Director ejecutivo y tecnológico en JA Modules. Experto programador PrestaShop y Experto programador WordPress.
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!!
You are a very smart person!
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!
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!
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!
As I website owner I believe the written content here is real fantastic , appreciate it for your efforts.
I know this site provides quality based posts and extra information, is there any other web site which presents such things in quality?
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.
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.