Solved

Accesing checkboxes using JS - named using 2D array

Posted on 2003-11-13
2
645 Views
Last Modified: 2008-02-01
Hi there:

I have some checkboxes generated on-the-fly on the server-side using PHP, and they're named using a 2D array. I'd like to have a little javascript perform a "check"/"uncheck" thing but cannot for the life of me get it to work.

Here's the HTML:
--------------------

<p><a href="#" onclick="checkAll('data','RSconsumer');">check all</a></p>
<form name="someform" method="post" action="somescript.php">
<input type="checkbox" name="data[RSconsumer][motoring]" checked="checked" />
<input type="submit" value="submit" />
</form>

BTW: the 2nd array value/name ("motoring" in this case) can be almost anything, (It's either "motoring","fashion" or whatever else comes out of the source array for generating these checkboxes)

Here's the JavaScript:
------------------------

function checkAll(checkbox_name,array_value)
{
      var form = document.forms[0];
      for(var i=0; i < form.elements.length; i++)
      {
            if((form.elements[i].type == 'checkbox') && (form.elements[i].name == 'checkbox_name['+array_value+'][]'))
            {
                  form.elements[i].checked = true;
            }
      }
}

If you can sort this one - I'd be v-e-r-y grateful :-)
Thanks a lot.
0
Comment
Question by:phptek
2 Comments
 
LVL 10

Expert Comment

by:Bustarooms
ID: 9739283
how about this


function checkAll(checkbox_name,array_value)
{
    var form = document.forms[0];
    for(var i=0; i < form.elements.length; i++)
    {
      var theCheckboxName = checkbox_name + '[' + array_value + ']';
         if((form.elements[i].type == 'checkbox') && (form.elements[i].name.indexOf(theCheckboxName) != -1))
          {
              form.elements[i].checked = true;
         }
    }
}
0
 
LVL 10

Accepted Solution

by:
NetGroove earned 250 total points
ID: 9739292
Here my proposal:


<form name="someform" method="post" action="somescript.php">
<p>
<input type="checkbox" onClick="checkAll(this, 'data[RSconsumer]')">check all</p>
<input type="checkbox" name="data[RSconsumer][motoring]" checked="checked" />
<input type="submit" value="submit" />
</form>
<script>
function checkAll(checkBox,checkPrefix) {
    var elem = checkBox.form.elements;
    for(var i=0; i < elem.length; i++)
    {
         if((elem[i].type == 'checkbox') && (elem[i].name.substr(0,checkPrefix.length) == checkPrefix))
          {
              elem[i].checked = checkBox.checked;
         }
    }
}
</script>


Be aware that the selectAll is now also a master checkbox and has to be inside the form section.

Good luck,
NetGroove

 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now