robert_gsfame Posted March 19, 2010 Share Posted March 19, 2010 I have problem when creating Check All to tick all the checkbox.. Let say i have 2 records from 3 available space that can be used to store the record, assume patient record patient A, patient C with all specific detail including name, address and everything and i put this when there is a record <input type=checkbox id=1>Delete ----------> for record A <input type=checkbox id=2>Delete ----------> for record B <input type=checkbox id=3>Delete ----------> for record C if there is no record, no checkbox will be appeared but <a href=insert_data.php>Insert Data</a> i have this : <a href="javascript:void(0)" onclick=" document.getElementById(1).checked=true; document.getElementById(2).checked=true; document.getElementById(3).checked=true;>check all</a> There is no problem when those 3 records are there, but if only A and C which means that the 2nd record is empty, then the checkall link will not do its function Can anyone help me? Thx Quote Link to comment Share on other sites More sharing options...
slurpee Posted March 19, 2010 Share Posted March 19, 2010 This is because the DOM element with id equal to '2' is null and your script won't execute after that statement. You need to add a check to see if it's null or not. <form> <input type="checkbox" name="test1" id="test1"> test 1<br /> <input type="checkbox" name="test3" id="test3"> test 3<br /> </form> <a href="javascript:;" onclick="checkAll();">test</a> <script> function checkAll() { if(document.getElementById('test1') !== null) document.getElementById('test1').checked = true; if(document.getElementById('test2') !== null) document.getElementById('test2').checked = true; if(document.getElementById('test3') !== null) document.getElementById('test3').checked = true; } </script> 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.