[Javascript] Auto deleselecting a check box

Paul Novitski paul at novitskisoftware.com
Thu Apr 29 13:39:01 CDT 2004


DEV,

Your "end function" syntax implies VBscript to me, but I'll answer your 
question in Javascript:

I'm making the script less complicated by assigning the 'Tell you later' 
checkbox an Id so I can change its status without having to search for it:

<input type=checkbox name=countriesbeen value='USA' 
onClick='UnCheckTellYouLater()'>USA
<input type=checkbox name=countriesbeen value='UK' 
onClick='UnCheckTellYouLater()'>UK
<input type=checkbox name=countriesbeen value='MEXICO' 
onClick='UnCheckTellYouLater()'>MEXICO
<input id="boxLater" type=checkbox name=countriesbeen value='Tell you 
later' onClick='UnCheckTellYouLater()'>Tell you later

function UnCheckTellYouLater()
{
         // for each checkbox...
         var aBoxes = document.getElementsByName("countriesbeen")
         for (iBox=0; iBox<aBoxes.length; iBox++)
         {
                 // if a country box has been checked...
                 if ( (aBoxes[iBox].checked) && (aBoxes[iBox].id != 
"boxLater") )
                 {
                         // uncheck the TYL box and end
                         boxLater.checked = false
                         break
                 }
         }
   }

A tangential advantage of testing for the checkbox's internal id instead of 
its value ('Tell you later') is that you won't have to tweak your code if 
you change your checkbox display text.

I suspect that if someone checks the Tell you later box you'll want to 
uncheck the various country boxes.  If this is the case, you could give the 
TYL box a different function:

<input id="boxLater" type=checkbox name=countriesbeen value='Tell you 
later' onClick='UnCheckCountries()'>Tell you later
...
function UnCheckCountries()
{
         // uncheck countries only if TYL is checked
         if (boxLater.checked)
         {
                 // for each checkbox...
                 var aBoxes = document.getElementsByName("countriesbeen")
                 for (iBox=0; iBox<aBoxes.length; iBox++)
                 {
                         // if it's a country box, uncheck it
                         if ( (aBoxes[iBox].id != "boxLater") )
                         {
                                 boxLater.checked = false
                         }
                 }
         }
   }

Paul


At 09:49 AM 4/29/2004, DEV wrote:
>Say you have a question like this ?
>
>Which countries have you been so far ?
>
>USA
>UK
>MEXICO
>TELL YOU LATER ( pre checked )
>
>When user selects USA/UK and/or MEXICO, the pre-checked [tell you later]
>option should be unchecked. How do you accomplish this ?
>
><input type=checkbox name=countriesbeen value='USA'
>onClick='UnCheckTellYouLater()'>USA
><input type=checkbox name=countriesbeen value='UK'
>onClick='UnCheckTellYouLater()'>UK
><input type=checkbox name=countriesbeen value='MEXICO'
>onClick='UnCheckTellYouLater()'>MEXICO
><input type=checkbox name=countriesbeen value='Tell you later'
>onClick='UnCheckTellYouLater()'>Tell you later
>
>
>function UnCheckTellYouLater()
>     //what do you do here to UnCheck Tell You Later
>end function
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list