javascript: ensuring array check boxes are checked

  • JackDaRippaZ
  • Newbie
  • Newbie
  • JackDaRippaZ
  • Posts: 11

Post 3+ Months Ago

Hi again peoples

Does anyone know how to use javascript to work through check boxes to see if they are checked:

MY CHECK BOXES ARE AS FOLLOWS:
Code: [ Select ]
<input type="checkbox" name="checkArrayBoxes[]" value="XXX"> XXX


how do i deal with these checkboxes that are an array??


i have the code below to deal with checkboxes that are detfined as
Code: [ Select ]
<input type="checkbox" name="normalCheckBoxes[]" value="XXX"> XXX


CODE TO HANDLE normal check boxes IS BELOW (it will see if they are selected if not it will display the alert message)


Code: [ Select ]
function submitMe(ticka) {
sel = -1;
for (i=0; i<ticka.normalCheckBoxes.length; i++) {
  if (ticka.normalCheckBoxes[i].checked) {
  sel = i;
  }
}
if (sel == -1) {
alert("you must select");
return false;
}
return true;
}
  1. function submitMe(ticka) {
  2. sel = -1;
  3. for (i=0; i<ticka.normalCheckBoxes.length; i++) {
  4.   if (ticka.normalCheckBoxes[i].checked) {
  5.   sel = i;
  6.   }
  7. }
  8. if (sel == -1) {
  9. alert("you must select");
  10. return false;
  11. }
  12. return true;
  13. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

:scratchhead: Huh ?

It may just be me but it looks like you have answered your own question...

I'm guessing your adding a new group of checkBoxes with the name checkarrayboxes to the same form that has normalcheckboxes

In that case, somthing close to this should work,
Code: [ Select ]
function submitMe(ticka) {
sel = -1;
sel2 = -1;
for (i=0; i<ticka.normalCheckBoxes.length; i++) {
  if (ticka.normalCheckBoxes[i].checked) {
  sel = i;
  }
}
for (i=0; i<ticka.checkArrayBoxes.length; i++) {
  if (ticka.checkArrayBoxes[i].checked) {
  sel2 = i;
  }
}
if(sel == -1 || sel2 == -1){
  if(sel == -1){
   alert("you must select from group one");
  }
  if(sel2 == -1){
     alert("you must select from group two");
  }
return false;
}
return true;
}
  1. function submitMe(ticka) {
  2. sel = -1;
  3. sel2 = -1;
  4. for (i=0; i<ticka.normalCheckBoxes.length; i++) {
  5.   if (ticka.normalCheckBoxes[i].checked) {
  6.   sel = i;
  7.   }
  8. }
  9. for (i=0; i<ticka.checkArrayBoxes.length; i++) {
  10.   if (ticka.checkArrayBoxes[i].checked) {
  11.   sel2 = i;
  12.   }
  13. }
  14. if(sel == -1 || sel2 == -1){
  15.   if(sel == -1){
  16.    alert("you must select from group one");
  17.   }
  18.   if(sel2 == -1){
  19.      alert("you must select from group two");
  20.   }
  21. return false;
  22. }
  23. return true;
  24. }
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

I think I know what you are talking about and I had the same problem:

Basically, for PHP, you need to include the square brackets in the checkbox name so you can receive the values as an array. But, how can you refer to or manipulate the checkboxes in Javascript when they have square brackets in their name?!

Like this:


Code: [ Select ]
myCheckboxArray = document.forms[0].elements["checkArrayBoxes[]"];


you can now use myCheckboxArray (or whatever variable name you use) as a javascript array of checkbox elements.

This technique should work for other form element types as well, for example if you have a form with a variable number of text fields with the same name.
  • JackDaRippaZ
  • Newbie
  • Newbie
  • JackDaRippaZ
  • Posts: 11

Post 3+ Months Ago

hey man,
yeah i tried that and it makes sense
i inserted the code above

Code: [ Select ]
function submitMe(ticka) {
sel = -1;

myCheckboxArray = document.forms[0].elements["checkArrayBoxes[]"];


for (i=0; i<ticka.normalCheckBoxes.length; i++) {
  if (ticka.normalCheckBoxes[i].checked) {
  sel = i;
  }
}
if (sel == -1) {
alert("you must select");
return false;
}
return true;
}
  1. function submitMe(ticka) {
  2. sel = -1;
  3. myCheckboxArray = document.forms[0].elements["checkArrayBoxes[]"];
  4. for (i=0; i<ticka.normalCheckBoxes.length; i++) {
  5.   if (ticka.normalCheckBoxes[i].checked) {
  6.   sel = i;
  7.   }
  8. }
  9. if (sel == -1) {
  10. alert("you must select");
  11. return false;
  12. }
  13. return true;
  14. }


but i keep getting a syntax error on the

Code: [ Select ]
if (ticka.normalCheckBoxes[i].checked) {


so i am assuming it is not handling the variable right? does it matter that i used 'ticka' as a parameter
  • JackDaRippaZ
  • Newbie
  • Newbie
  • JackDaRippaZ
  • Posts: 11

Post 3+ Months Ago

thanks after a bit of sorting i managed to get it, thanks to everyone who replied espec Cafu, thanks mate

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 81 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.