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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.