Jump to content

Check All problem


robert_gsfame

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/195766-check-all-problem/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/195766-check-all-problem/#findComment-1028506
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.