I am in need of making a jquery based interface where on change of select causes appropriate checkeboxes to be marked. For eg I am listing Group in a drop down each group have its members. For eg group A have 5 members. Every Member is listed when A user select groupA all checkbox(5 users) checkbox will get auto selected
Here is what I have now
<?php
$groups[]= array(12 => array ( 1,2,3,4,5),13=>array ( 32,25,26),14=>array(22,23));
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
function selecttheuser()
{
$(document).ready(function() {
var gidet=$("select#group_id").val();
alert(gidet);
});
}
</script>
<select style="width:466px" name="group_id" id="group_id" onchange="selecttheuser()">
<option value="0">Not a Group Event</option>
<option value="12">Gname</option>
<option value="13">Groupname2</option>
</select>
<input type="checkbox" value="19" class="frnzchk" name="frnzchk[]"><br>
<input type="checkbox" value="20" class="frnzchk" name="frnzchk[]"><br>
<input type="checkbox" value="21" class="frnzchk" name="frnzchk[]"><br>
<input type="checkbox" value="22" class="frnzchk" name="frnzchk[]"><br>
<input type="checkbox" value="23" class="frnzchk" name="frnzchk[]"><br>
<input type="checkbox" value="32" class="frnzchk" name="frnzchk[]"><br>
<input type="checkbox" value="25" class="frnzchk" name="frnzchk[]"><br>
<input type="checkbox" value="26" class="frnzchk" name="frnzchk[]"><br>
I have 2 problems
- how can I manage the PHP array it can have like 100 groups so how do I make this feasible in javascript
- How to select corresponding checkboxes
Sorry If this seems straight but I donot have any idea how to approach this
Here’s how I would have solve this problem.
First I’ll show you the working code and then I’ll explain it after.
Here’s the first chunk:
This is your list of groups and the choices associated with them. Notice when you declare the array you don’t need the square brackets i.e.
$groups = array()instead of$groups[] = array().Next I put in all of the plumbing for a complete HTML5 page that will validate[1]. I moved your CSS out of the HTML because it makes it easier to organize, and in practice this should be moved to a completely separate CSS file.
Here’s the next important chunk:
This loops through each of your groups, and creates a
DIVto hold all of the choices for that group. The div is identified by the idgroup_12,group_13, orgroup_14; The numbers come from the keys used in the$groupsarray.Next is the Javascript:
I placed this all at the end of the body because Javascript runs in a single thread, so this means if you load it up front like you had it, nothing else can load until the javascript has finished loading[2]. In this example it’s not a big deal, but when you have a lot of javascript it can make a big difference!
You’ll notice that I wrapped everything inside
$(document).ready(function(){})– this is because you don’t want it to work until everything is loaded. Since you had$(document).ready(function(){})inside your function, every time that function gets called, it will check to see if everything is loaded, and if it’s not, then you won’t see anything happen.I bound the
change()function to theselectelement, which means it will be called whenever theselectelement is changed, and the code inside that function will be called. We reset all of the choices, and then select all of the choices that are contained in the appropriate div.That should do it! Happy coding!
[1] – http://validator.w3.org
[2] – https://stackoverflow.com/a/1108245/1100835