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.
21 11066 @mukeshrasm
you can count the number of checked checkboxes. use getElementsByName() to get all boxes and test the checkbox.checked property for each one.
@Dormilich
How can I count the number of checked checkboxes.
@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(), ...).
@Dormilich
I am doing in this way: -
function check()
-
{
-
var a=document.getElementsByName("checkbox"
-
var j=0
-
for(i=0;i<=a.length;i++)
-
{
-
if(a[i].checked==true)
-
{
-
j=j+1;
-
}
-
}
-
if (j==0)
-
{
-
alert("please select checkbox")
-
}
-
-
}
-
but it is not working. not sure where I am wrong
@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++;
@Dormilich
here is the html -
<form id="form1" name="form1" method="post" action="" >
-
<input type="checkbox" name="checkbox" value="checkbox" />
-
</p>
-
<p>
-
<input type="checkbox" name="checkbox" value="checkbox" />
-
</p>
-
<p>
-
<input type="checkbox" name="checkbox" value="checkbox" />
-
</p>
-
<p>
-
<input type="checkbox" name="checkbox" value="checkbox" />
-
<input type="submit" name="Submit" value="Submit" onclick="check()" />
-
</p>
-
</form>
-
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)
@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.
nothing special... - function check()
-
{
-
var a=document.getElementsByName("checkbox");
-
var j=0;
-
for (i=0, l=a.length; i<l; i++)
-
{
-
if (a[i].checked==true)
-
{
-
j++;
-
}
-
}
-
if (j==0)
-
{
-
alert("please select checkbox")
-
}
-
// this is probably where you got no response
-
alert(j);
-
}
@Dormilich
Yes!
Now it is working fine, but why I need to initializ twice in for loop
initializing twice??? which variable do you mean?
@Dormilich -
for (i=0, l=a.length; i<l; i++)
-
cann't I use like I previous one
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
@Dormilich
Thanks!
Actually, I was unware of this minute information.
@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 -
var ch=document.getElementById("checkdel")
-
var chh=ch.length
-
alert(chh)
-
but it is showing as 'undefined'.
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).
@Dormilich
actually I am doing it dynamically using php
I guess you'll have to check it in PHP anyways.....
@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
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)
@Dormilich
Thanks for encouraging words and I am trying.
Sign in to post your reply or Sign up for a free account.
Similar topics
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!
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
###############
...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |