473,327 Members | 2,055 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,327 software developers and data experts.

Looping through form elements

24
Hi,

I have 10 checkboxes named chk1, chk2, chk3,.., chk10

I'd like to loop through them, so I wrote this code but it dosen't work:

Expand|Select|Wrap|Line Numbers
  1. for (i = 1; i <= 10; i++)
  2. {
  3.     if(document.Form1.chk+i.checked == true)
  4.     {
  5.         alert("check box number: "+ i + " is checked");
  6.     }
  7. }
  8.  
Please help me! Thanks in advance.
Jan 11 '11 #1
3 846
RamananKalirajan
608 512MB
Hi goodamr,
Have the same name for hos checkboxes, with that u can access it easily

ex: HTML

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="chk" id="chk1" value="check1" />
  2. <input type="checkbox" name="chk" id="chk2" value="check1" />
  3.  
likewise for 10 check boxes.

In JS:

Expand|Select|Wrap|Line Numbers
  1. var chkBox = document.getElementsByName("chk");
  2. for(var i=0;i<chkBox.length;i++){
  3.    if(chkBox[i].checked==true)
  4.       alert(i+" check box was selected");
  5. }
  6.  
Thanks and Regards
Ramanan Kalirajan
Jan 11 '11 #2
goodamr
24
Hi Ramanan,

Thanks alot for your reply. I found the solution, instead of giviing the checkboxes names, I gave them id names, ex. id="chk1" . and used that code:

Expand|Select|Wrap|Line Numbers
  1. for (i = 1; i <= 10; i++)
  2. {
  3.     if(document.getElementById('chk'+i).checked == true){}
  4. }
  5.  
Also, I'll give your code a try.
Jan 11 '11 #3
Dormilich
8,658 Expert Mod 8TB
you can also use names …
Expand|Select|Wrap|Line Numbers
  1. document.Form1["chk"+i].checked
Jan 11 '11 #4

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

Similar topics

1
by: Anna | last post by:
Hi all. I have a javascript function that loops throught all text boxes inside a form: var elems = document.formName.elements; for(i = 0; i < elems.length; i++) { if(elems.type &&...
5
by: Richard Cornford | last post by:
I am interested in hearing opinions on the semantic meaning of FORM (elements) in HTML. I have to start of apologising because this question arose in a context that is not applicable to the...
1
by: Howard Jess | last post by:
Apparently, form elements of type <input type="image" src="...> are not included in the form's elements collection. I don't understand why not; according to DOM2, all form control elements...
12
by: Pudlik, Szymon | last post by:
Hi, I've written some code: function onSubmit(form){ for (var i = 0; i < form.elements.lenght; i++){ if (form.elements.disabled == 1) form.elements.disabled = 0; }
2
by: Brett | last post by:
The following code will allow me to loop through FORM tags but not the elements in them. I may have five forms on one page. How do I loop through form elements in the forth FORM? private...
25
by: Dave | last post by:
Hello. In trying to get an anchor element to stylistically match an input or button element, I find that the button and input cannot be styled according to the 2.1 CSS spec. For example, I...
1
by: Mel | last post by:
I need to loop through all form elements such as text, radios, check boxes and the like and to save their state in a file. does anyone have JS code to do this ? It can help a bunch thanks
2
by: DoomedLung | last post by:
Hey, I'm in the process of writing a function to check each element of a form isn't empty. I've come to a dead end.. Here is my function: function validateData(){
1
by: raldred | last post by:
hey. Im creating a basic function to submit product skus and quantities to another page i am created the form elements for the quantities in a popup div based on items selected on the current page....
2
by: mars123 | last post by:
hi, I am facing a js error in my code, below is the prob. statement I have a radio2 javascript function as below, it works like this.. When a parent radio button is selected only one of its...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.