I need to select children checkboxes when selecting the parent one. This is my function:
function SelectChildrens(checkbox_name){ form = document.forms[0]; Sname = checkbox_name.split("-"); for (i=0;i<form.elements.length;i++){ THATname = form.elements[i].name.split("-"); if (Sname.length==1){ if (THATname[0]==Sname[0]){ form.elements[i].checked=!form.elements[i].checked; } } if (Sname.length==2){ if (THATname[1]==Sname[1]){ form.elements[i].checked=!form.elements[i].checked; } } }//endof for
}
I've got parent checkbox which is: <input type=checkbox name=FISH-FILTERS onclick="SelectChildrens(this.name)"> and childrens like: <input type=checkbox name="products_id[]" value="2"> <input type=checkbox name="products_id[]" value="3"> <input type=checkbox name="products_id[]" value="4"> <input type=checkbox name="products_id[]" value="5">
what can I add to children checkboxes so that the function works fine ?
fea...@multicon.pl wrote: > I need to select children checkboxes when selecting the parent one. > This is my function:
> function SelectChildrens(checkbox_name){ > form = document.forms[0];
If you pass a reference to the checkbox that is clicked on it is easier to get the form (see below).
> Sname = checkbox_name.split("-");
I can't work out what you are trying to do here, so I've posted a completely new function below. You have put the onclick on an element with a name that is nothing like that of the 'childrens' checkboxes.
function SelectChildrens(x){ var el, els = x.form.elements; var m, n = x.name.split('_')[1]; for(var i=0, j=els.length; i<j; i++){ el = els[i]; if ('checkbox' == el.type) { if (el.name && ( m = el.name.split('_')[1]) && m == n ){ el.checked = x.checked; } } } }
</script> </head> <body>
<form action=""> <p>Selecting this checkbox will select all the other checkboxes <br> <input type="checkbox" name="products_id[1]" onclick="SelectChildrens(this)">select the 1's<br>
It is more code than is required to handle your specific case (although it is less than 4k), but it is more generalized and may come in handy in other similar situations or in your current situation if the requirements change slightly.
JRS: In article <1116014380.495870.326...@g44g2000cwa.googlegroups.com> , dated Fri, 13 May 2005 12:59:40, seen in news:comp.lang.javascript, RobB <fernd...@hotmail.com> posted :
If you were to try that in a web-authoring group, I suspect that you would be told that setting fonts to absolute sizes is BAD - and in conflict with accessibility principles. Even if you had used 11pt.
Setting widths in px is also bad, AIUI, except for boxing graphics of known size.