By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

how to display message if none of the checkbox is selected

100+
P: 254
Hi

I am deleting records from database using checkbox. means only that record will be deleted from database which are selected in checkbox. and before deleting record I am displaying a message "Are you sure to delete the selected records" using javascript.and when user clicks OK, record is deleted.

Here in message I want it should like "Are you sure to delete 5 records" if user selects 5 records. So how can I do this using javascript.

And if user without selecting any checkbox and clicks Delete button then it should display message "no record is selected. please select" using javascript so how can I do this in javascript.

Name of the checkbox is chkdelete.
Mar 26 '09 #1
Share this Question
Share on Google+
21 Replies


Dormilich
Expert Mod 5K+
P: 8,639
@mukeshrasm
you can count the number of checked checkboxes. use getElementsByName() to get all boxes and test the checkbox.checked property for each one.
Mar 26 '09 #2

100+
P: 254
@Dormilich
How can I count the number of checked checkboxes.
Mar 26 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
@mukeshrasm
getElementsByName("checkdelete") will give you all the checkboxes (as array*). loop through this array (using for-loop) and test if the checked property is true (checked) or false (unchecked), if it is true, increment you counter variable (for checked checkboxes).

* it is actually a HTMLElementCollection, which is an Object (with numerical properties) rather than an Array. The difference is that this object doesn't contain most methods of the Array object (like pop(), push(), shift(), ...).
Mar 26 '09 #4

100+
P: 254
@Dormilich

I am doing in this way:
Expand|Select|Wrap|Line Numbers
  1. function check()
  2. {
  3.     var a=document.getElementsByName("checkbox"
  4.     var j=0
  5.         for(i=0;i<=a.length;i++)
  6.         {
  7.             if(a[i].checked==true)
  8.             {
  9.                  j=j+1;
  10.             }
  11.         }
  12.        if (j==0)
  13.        {
  14.           alert("please select checkbox")
  15.        }
  16.  
  17. }
  18.  
but it is not working. not sure where I am wrong
Mar 26 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
@mukeshrasm
hard to tell without the HTML code.

check if the variables you refer to actually exist, plus you didn't specify an action if at least one checkbox was selected.

PS: you can replace j = j+1; by j++;
Mar 26 '09 #6

100+
P: 254
@Dormilich
here is the html
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="" >
  2.  <input type="checkbox" name="checkbox" value="checkbox" />
  3.   </p>
  4.   <p>
  5.     <input type="checkbox" name="checkbox" value="checkbox" />
  6.   </p>
  7.   <p>
  8.     <input type="checkbox" name="checkbox" value="checkbox" />
  9.   </p>
  10.   <p>
  11.     <input type="checkbox" name="checkbox" value="checkbox" />
  12.  <input type="submit" name="Submit" value="Submit" onclick="check()" />
  13.   </p>
  14. </form>
  15.  
Mar 26 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
it's working fine for me... what do you expect that is not working?

PS: I corrected some writing mistakes, though.... (syntax errors will show up in the Error Console)
Mar 26 '09 #8

100+
P: 254
@Dormilich
what are those corrections if you can please suggest me and how it could possible that it is working in your end but not in my.
Mar 26 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
nothing special...
Expand|Select|Wrap|Line Numbers
  1. function check()
  2. {
  3.     var a=document.getElementsByName("checkbox");
  4.     var j=0;
  5.         for (i=0, l=a.length; i<l; i++)
  6.         {
  7.             if (a[i].checked==true)
  8.             {
  9.                  j++;
  10.             }
  11.         }
  12.        if (j==0)
  13.        {
  14.           alert("please select checkbox")
  15.        }
  16. // this is probably where you got no response
  17.  alert(j);
  18. }
Mar 26 '09 #10

100+
P: 254
@Dormilich
Yes!
Now it is working fine, but why I need to initializ twice in for loop
Mar 26 '09 #11

Dormilich
Expert Mod 5K+
P: 8,639
initializing twice??? which variable do you mean?
Mar 26 '09 #12

100+
P: 254
@Dormilich
Expand|Select|Wrap|Line Numbers
  1. for (i=0, l=a.length; i<l; i++)
  2.  
cann't I use like I previous one
Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<=a.length;i++)
  2.  
Mar 26 '09 #13

Dormilich
Expert Mod 5K+
P: 8,639
of course you can, but the disadvantage is that you have to compute the array length every time anew, while the first one computes the length only once.

and another one: use count_var < array_length (as terminating condition), otherwise you'll access a non-existing element, which may cause your script to break
Mar 26 '09 #14

100+
P: 254
@Dormilich
Thanks!

Actually, I was unware of this minute information.
Mar 26 '09 #15

100+
P: 254
@Dormilich
Hi

Reference to post #7 I want to find the number of checkboxes in the form. I gave id to checkbox "checkdel" and to that I used like this

Expand|Select|Wrap|Line Numbers
  1. var ch=document.getElementById("checkdel")
  2. var chh=ch.length
  3. alert(chh)
  4.  
but it is showing as 'undefined'.
Mar 27 '09 #16

Dormilich
Expert Mod 5K+
P: 8,639
of course. a HTMLInputElement (and its parent interface HTMLElement) does not have a length property (why should it, you know that there is only one).
Mar 27 '09 #17

100+
P: 254
@Dormilich
actually I am doing it dynamically using php
Mar 27 '09 #18

Dormilich
Expert Mod 5K+
P: 8,639
I guess you'll have to check it in PHP anyways.....
Mar 27 '09 #19

100+
P: 254
@Dormilich
ok thanks! but I am not sure what will be title of my posting or how can I reference my this posting to php
Mar 27 '09 #20

Dormilich
Expert Mod 5K+
P: 8,639
you can always put a link to this thread in your question.

although I don't understand why you've given up on this little script. you've nearly had it finished...

advice for PHP : if you name your checkboxes like name="checkbox_name[]" all the checkboxes bearing this will be put into an array.

some further reading on HTML DOM object types:HTML IDL Definitions (these tell you, which methods and properties are available from the DOM side)
Mar 27 '09 #21

100+
P: 254
@Dormilich
Thanks for encouraging words and I am trying.
Mar 27 '09 #22

Post your reply

Sign in to post your reply or Sign up for a free account.