I’m trying to create a check all / uncheck all function for a series of check boxes. Seems easy, but for some reason the brackets used in the check_box_tag don’t play well with javascript functions
Here’s a real basic example of what I’m trying to do:
…but that won’t check any of the boxes. If I remove the brackets from the check_box_tag for delete_contact and add_to_group all the check boxes get checked, but then I don’t get the array of ids in params[‘delete_contact’] or params[‘add_to_group’]
When I remove the brackets the check boxes get checked but I lose the array
of ids.
<%= check_box_tag('delete_contact', contact.id) -%>
<a href="#" onClick="checkAll(document.contact_list.delete_contact);return
false;" >Check All</a>
-Dave
>
>
> View:
> > <script type="text/javascript">
> > function checkAll(field) {
> > for (var i = 0; i < field.length; i++)
> > field[i].checked = true ;}
> >
> > function uncheckAll(field) {
> > for (var i = 0; i < field.length; i++)
> > field[i].checked = false ;}
> > </script>
> >
> >
> I admit I didn't take the time to fully analyze your problem just noticed
> one thing, you're not closing the function brackets. Should be:
>
> <script type="text/javascript">
> function checkAll(field) {
> for (var i = 0; i < field.length; i++){
> field[i].checked = true ;
> }
> }
>
> function uncheckAll(field) {
> for (var i = 0; i < field.length; i++){
> field[i].checked = false ;
> }
> }
> </script>
>
> Again...no deep analysis so this may not be the only problem, but it is a
> problem.
>
> good luck,
> andy
> --
> Andrew Stone
> >
>
I use these functions...
function toggleAll(name)
{
boxes = document.getElementsByClassName(name);
for (i = 0; i < boxes.length; i++)
if (!boxes[i].disabled)
{ boxes[i].checked = !boxes[i].checked ; }
}
function setAll(name,state)
{
boxes = document.getElementsByClassName(name);
for (i = 0; i < boxes.length; i++)
if (!boxes[i].disabled)
{ boxes[i].checked = state ; }
}
Pretty sure these will work with controls that have '' in their
names.
Forgive me for the javascript noob question. Your toggleAll(name) function is probably what I should be using. I tried using it and I still get a syntax error:
Forgive me for the javascript noob question. Your toggleAll(name) function
is probably what I should be using. I tried using it and I still get a
syntax error:
JavaScript syntax says that you're trying to treat delete_contact as an
object and look up the property with the name ...oops, you didn't
supply a name. Hence the error.
In JavaScript, as in Lua, you can reference any property of any object
in two ways:
1) Using standard 'dot' notation, as you have above:
alert( foo.bar );
2) Using bracket notation (as the system thought you were trying to
do):
alert( foo[ "bar" ] );
This latter method is useful when you have the name of the property you
want stored in another variable:
var prop = "bar";
alert( foo[ prop ] );
or (as in your case) when the name of the property is not a valid
JavaScript identifier:
checkAll( document.contact_list[ "delete_contact" ] );