I'm trying to create a select all button using js on a bunch of checkboxes that are generated by a foreach function.
This is what the script for the select all/deselect all functions end up being:
<script>
function selectAll() {
document.getElementById('abbsmalone_select').checked = true;
document.getElementById('cchkorea_select').checked = true;
document.getElementById('amygrant_select').checked = true;
document.getElementById('long_select').checked = true;
}
</script>
<script>
function deselectAll() {
document.getElementById('abbsmalone_select').checked = false;
document.getElementById('cchkorea_select').checked = false;
document.getElementById('amygrant_select').checked = false;
document.getElementById('long_select').checked = false;
}
</script>
Here's the actual PHP script to create the functions:
<script>
function selectAll() {
<?PHP
get_friends_select();
foreach ($_SESSION['friends'] as $friend) {
echo "document.getElementById('" . $friend . "_select').checked = true;
";
}
?>
}
</script>
<script>
function deselectAll() {
<?PHP
get_friends_select();
foreach ($_SESSION['friends'] as $friend) {
echo "document.getElementById('" . $friend . "_select').checked = false;
";
}
?>
}
</script>
Here's the code for the buttons:
<input name="deselect_all" type="button" class="form_button2" onclick="deselectAll()" value="select all" style="float: left;">
<input name="select_all" type="button" class="form_button2" onclick="selectAll()" value="deselect all" style="float: left;">
Neither button works. Can you not do it this way? Or is my code wrong? :-/
UPDATE:
As suggested, I tried the following but it's still not working.
<input type="checkbox" class="friend_select" name="abbsmalone_select" id="abbsmalone_select" value="yes" checked='checked' style="display: none;">
function selectAll() {
jQuery(".friend_select").prop('checked', true);
}
function deselectAll() {
jQuery(".friend_select").prop('checked', false);
}