Dysan Posted December 27, 2007 Share Posted December 27, 2007 I have the following PHP code that creates a simple 2x2 table, and features check boxes. Upon clicking the top (title) checkbox, how do I check/uncheck each of the records check boxes? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die(mysql_error()); } mysql_select_db("db", $con); $result = mysql_query("SELECT * FROM person"); echo '<table border="1"> <tr> <th><input type="checkbox" name="checkbox" value="checkbox"></th> <th>First Name</th> <th>Last Name</th> </tr>'; while($row = mysql_fetch_array($result)) { echo '<tr>'; echo '<td><input type="checkbox" name="checkbox" value="checkbox"></td>'; echo '<td>'.$row['FirstName'].'</td>'; echo '<td>'.$row['LastName'].'</td>'; echo '</tr>'; } echo '</table>'; mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 27, 2007 Share Posted December 27, 2007 Give the table and the check all button unique ids and this code should work function checkAll(){ var table_eles = document.getElementById('table_id').getElementsByTagName('input'); var action = document.getElementById('check_all_id').checked ? true : false; foreach(var x in table_eles){ if(table_eles[x].type == 'checkbox'){ table_eles.checked = action; } } } and you can do some inline js on your input field <input type="checkbox" name="checkbox" id="check_all_id" onclick="checkAll();" /> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.