473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to display message if none of the checkbox is selected

254 100+
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
21 11066
Dormilich
8,658 Expert Mod 8TB
@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
mukeshrasm
254 100+
@Dormilich
How can I count the number of checked checkboxes.
Mar 26 '09 #3
Dormilich
8,658 Expert Mod 8TB
@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
mukeshrasm
254 100+
@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
8,658 Expert Mod 8TB
@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
mukeshrasm
254 100+
@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
8,658 Expert Mod 8TB
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
mukeshrasm
254 100+
@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
8,658 Expert Mod 8TB
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
mukeshrasm
254 100+
@Dormilich
Yes!
Now it is working fine, but why I need to initializ twice in for loop
Mar 26 '09 #11
Dormilich
8,658 Expert Mod 8TB
initializing twice??? which variable do you mean?
Mar 26 '09 #12
mukeshrasm
254 100+
@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
8,658 Expert Mod 8TB
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
mukeshrasm
254 100+
@Dormilich
Thanks!

Actually, I was unware of this minute information.
Mar 26 '09 #15
mukeshrasm
254 100+
@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
8,658 Expert Mod 8TB
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
mukeshrasm
254 100+
@Dormilich
actually I am doing it dynamically using php
Mar 27 '09 #18
Dormilich
8,658 Expert Mod 8TB
I guess you'll have to check it in PHP anyways.....
Mar 27 '09 #19
mukeshrasm
254 100+
@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
8,658 Expert Mod 8TB
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
mukeshrasm
254 100+
@Dormilich
Thanks for encouraging words and I am trying.
Mar 27 '09 #22

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

Similar topics

4
by: Targa | last post by:
I have a form in which I want to have an button(image) displayed next to a checkbox when it is checked. If the box is unchecked, the button should disappear. How can I do this? Thanks!
10
by: DettCom | last post by:
Hello, I would like to be able to display or hide fields based on whether a specific Yes/No radio button is selected. This is in conjunction with a posting a just made here in the same group...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
3
by: shreddie | last post by:
Could anyone assist with the following problem? I'm using JavaScript to hide/show table rows depending on the option selected in radio buttons. The script works fine in IE but in Firefox the...
3
by: Scott | last post by:
Relative newbie here, I'm looking to display the value of radio buttons and check boxes on the page before submission. So far I can do most of it. When "Hat" is checked there are to be no color...
1
by: rbinington | last post by:
Hi, I am trying to write a DNN module that has the ability to insert articles into an article repository. I want the users to be able to move pages around and enter text into the FCKEditor. I...
7
by: khinester | last post by:
Hello, I have the following template that basically does the following: User select Country, then a sub-list is generated with Regions and then this returns the Counties ############### ...
0
by: remya1000 | last post by:
I have a field called Departments in my database. and I have 3 monitors. So when Page_Load, I need to check number of departments I have in database. And depends upon that number of departments I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.