Hi,

I am not very good at javascript and I need help. this is probably not a difficult one for experience javascript users.

I have page which I have included in this post. A Check All/Uncheck All checkbox should check all the other checkboxes (I've skipped the detail of that) and then click "Delete" button should delete all the selected items (I've skipped the delete part also).

When I handle the delete, I first prompt user to make sure if he/she wants to delete the item (as my code indicates), but currently, the same prompt is also used if there is no selection. I would like to have a different prompt like "no seletion is made" when there is no checkbox is clicked. How do I verify if there is no checkbox is clicked in the handleDelete function?

Appreciated if somebody could help.

<html>
<head><title>test</title>

<script type="text/javascript">
<!--
function handleDelete()
{
if(confirm("Are you sure you want to delete the profiles?"))
{
document.forms[0].action.value = "delete";
document.forms[0].submit();
}
}
-->
</script>
</head>

<body>
<form name="form_name" method="POST" action="">
<table>
<tr>
<td><input type="button" name="delete" value="Delete" onclick="javascript:handleDelete();">&nbsp;</td>
</tr>
<tr>
<td><input type="checkbox" name="delete[]" value="0" onclick="toggleChecked(this)">Select All/Unselect All</td>
<tr>
<tr>
<td><input type="checkbox" name="delete[]" value="1">Apple</td>
</tr>
<tr>
<td><input type="checkbox" name="delete[]" value="2">Pear</td>
</tr>
<tr>
<td><input type="checkbox" name="delete[]" value="3">Orange</td>
</tr>
<tr>
<td><input type="checkbox" name="delete[]" value="1">Grapes</td>
</tr>
</table>
</form>
</body>
</html>

    <html>
    <head>
    <script language="javascript">
    function handleDelete(){
    flg = 0;
    for(i=0;i<document.form_name.check.length;i++){
    if(document.form_name.check.checked == true){
    flg = 1;
    break;
    }else{
    flg = 0;
    }
    }

    		if(flg == 0){
    			alert("nothing checked");
    			return false;
    		}
    	}
    </script>

    </head>
    <body>
    <form name="form_name" method="POST" action="">
    <table>
    <tr>
    <td><input type="button" name="ex" value="Delete" onclick="javascript:handleDelete();">&nbsp;</td>
    </tr>
    <tr>
    <td><input type="checkbox" name="check" value="0" onclick="toggleChecked(this)">Select All/Unselect All</td>
    <tr>
    <tr>
    <td><input type="checkbox" name="check" value="1">Apple</td>
    </tr>
    <tr>
    <td><input type="checkbox" name="check" value="2">Pear</td>
    </tr>
    <tr>
    <td><input type="checkbox" name="check" value="3">Orange</td>
    </tr>
    <tr>
    <td><input type="checkbox" name="check" value="1">Grapes</td>
    </tr>
    </table>
    </form>
    </body>
    </html>

      ashwin, thanks for the code. But it didn't work for me. But I got it working. Here's the working code. Try that.

      <html>
      <head>
      <script language="javascript">
      <!--
      function handleDelete(f)
      {
      var group = f.elements["delete[]"];
      var x, len = group.length;
      for(x=0; x<len; x++)
      {
      if(group[x].checked)
      {
      break;
      }
      }

      if(x < len)
      {
          if(confirm("Are you sure you want to delete the selected items?"))
          {
              f.page_action.value = "delete";
              f.submit();
          }
      }
      else
      {
          alert("nothing selected!");
          return false;
      }
      
      return true;

      }
      -->
      </script>

      </head>
      <body>
      <form name="form_name" method="POST" action="">
      <table>
      <tr>
      <td><input type="button" name="del" value="Delete" onclick="javascript:handleDelete(this.form);">&nbsp;</td>
      </tr>
      <tr>
      <td><input type="checkbox" name="delete[]" value="0" onclick="toggleChecked(this)">Select All/Unselect All</td>
      <tr>
      <tr>
      <td><input type="checkbox" name="delete[]" value="1">Apple</td>
      </tr>
      <tr>
      <td><input type="checkbox" name="delete[]" value="2">Pear</td>
      </tr>
      <tr>
      <td><input type="checkbox" name="delete[]" value="3">Orange</td>
      </tr>
      <tr>
      <td><input type="checkbox" name="delete[]" value="4">Grapes</td>
      </tr>
      </table>
      </form>
      </body>
      </html>

        Write a Reply...