?
Solved

Accesing checkboxes using JS - named using 2D array

Posted on 2003-11-13
2
Medium Priority
?
651 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 1000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
Suggested Courses

770 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