Solved

Accesing checkboxes using JS - named using 2D array

Posted on 2003-11-13
2
649 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

738 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