Solved

Accesing checkboxes using JS - named using 2D array

Posted on 2003-11-13
2
644 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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

743 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

18 Experts available now in Live!

Get 1:1 Help Now