Solved

Accesing checkboxes using JS - named using 2D array

Posted on 2003-11-13
2
648 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

828 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