Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unselecting dynamically generated checkboxes. 1

Status
Not open for further replies.

webmigit

Programmer
Aug 3, 2001
2,027
US
I have a cold fusion script that generates items and a checkbox..

Code:
<cfoutput query=&quot;getx&quot; group=&quot;AnimalClass&quot;>
  #AnimalClass# (Cat, Dog, Fish)<br>
  <cfoutput group=&quot;AnimalType&quot;>
    <input type=&quot;checkbox&quot; name=&quot;atype&quot;<cfif guessTypes contains AnimalType>checked</cfif>>#AnimalType# (Sheperd, Lab, Poodle)<br>
  </cfoutput><br>
</cfoutput>

Would generate roughly..
[ ] = checkbox, [x] = checked checkbox

Code:
Cat 
  [ ] Calico
  [x] Persian
Dog
  [x] Sheperd
  [x] Lab
  [ ] Poodle
  [ ] Terrier
  [x] Grey hound
Fish
  [x] Goldfish

What I want to do is have a uncheck link next to each class...

Code:
Cat               Uncheck all
  [ ] Calico
  [x] Persian
Dog                     Uncheck all
  [x] Sheperd
  [x] Lab
  [ ] Poodle
  [ ] Terrier
  [x] Grey hound
Fish                    Uncheck all
  [x] Goldfish

The trouble is all the checkboxes have the same name..

So I was hoping someone could help me out.. I can create a hidden field with a list of all the values of for that subgroup... like <input type=&quot;hidden&quot; name=&quot;listDog&quot; value=&quot;Sheperd,Lab,Poodle,Terrier,Greyhound&quot;> so like a javascript could be ran by
UncheckGroup(document.getElementById('listDog'))

The values are actually going to be numbers.. the IDs of the item but using names for simplicities sake...

Any ideas at all? Anyone?

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Since all of your checkboxes have the same name, they automaticaly create a control array. You can loop through it and just uncheck all checked ones. Assumming that your checkboxes are surrounded by the <form name=&quot;myForm&quot;> tag:
Code:
function uncheckAll(){
 for(i=0;i<document.myForm.atype.length;i++)
 {
  if(document.Form1.chk[i].checked) document.Form1.chk[i].checked = false;
 }
}

<input type=button value=&quot;Uncheck All&quot; onclick=&quot;javascript:uncheckAll()&quot;>
 
Great... How do I make this modification though?

Code:
function uncheckAll(){
 for(i=0;i<document.myForm.atype.length;i++)
 {
    if document.Form1.chk[i].value in &quot;comma,delimited,list
      if(document.Form1.chk[i].checked) document.Form1.chk[i].checked = false;
 }
  endif
}

Thanks for getting me started, you get a star.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Code:
for(i=0;i<document.myForm.atype.length;i++)
{
  var myList = &quot;1,2,3&quot;;
  var listArray = myList.split(&quot;,&quot;);

  for(j=0;j<listArray.length;j++){
   if(document.myForm.chk[i].value == listArray[j]){
     document.myForm.chk[i].checked = false;
     break;
   }
  }
}
Please give it a shot, I didn't test it.
 
Ah thanks,

But that second one didn't work... and I don't know enough js to really tell you why..

But all is appreciated.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Let me try to run a sample, I'll get back to you.
 
Sorry, that was my typo - &quot;chk&quot; should be &quot;atype&quot;:
Code:
function uncheckAll(){
 for(i=0;i<document.myForm.atype.length;i++)
 {
  var myList = &quot;1,2,3&quot;;
  var listArray = myList.split(&quot;,&quot;);

  for(j=0;j<listArray.length;j++){
   if(document.myForm.atype[i].value == listArray[j]){
    document.myForm.atype[i].checked = false;
    break;
   }
  }
 }
}
 
Code:
<script>
  function uncheckAll(strlist,bool){
    for(i=0;i<document.RCOform.atype.length;i++)
    {
      var myList = strlist;
      var listArray = myList.split(&quot;,&quot;);
      for(j=0;j<listArray.length;j++){
        if(document.RCOform.atype[i].value == listArray[j]){
          document.RCOform.atype[i].checked = bool;
          break;
        }
      }
    }
  }
</script>

Strlist is the list of values and bool is true or false..

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Perfectly..

Could not have done it with you, I just used a few of my own mods... like bool so that I could use it for the reverse effect..

If I could give you another star, I would!

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Cool, glad it worked. You are a qick learner though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top