having problems with checkboxes

Results 1 to 2 of 2

Thread: having problems with checkboxes

  1. #1
    Join Date
    Dec 1969

    Default having problems with checkboxes

    can anyone explain why this code doesnt work for me.<BR><BR>var checkTrue = 0;<BR>for (var i = 1; i &#060; 8; i++)<BR>{<BR> alert(document.rfsform.projtype[i].value)<BR>}<BR>if(("" + checkTrue) == "0")<BR>{<BR> alert("You must select a type of project!");<BR> return false;<BR>}<BR><BR><BR>thanks

  2. #2
    Join Date
    Dec 1969

    Default Not checkboxes problem at all...

    I&#039;d say it&#039;s just a basic coding problem.<BR><BR>You never change the value of checkTrue in your loop, so it will *always* be zero. So you will always give the message.<BR><BR>Why be surprised?<BR><BR>You have other problems in that code:<BR>(1) You run your loop from 1 to 7. But form elements of the same name are numbered starting from zero in the browser DOM (Document Object Model). You need to run your loop from 0 to whatever.<BR>(2) You are checking the ".value" property of the checkboxes. But *ALL* checkboxes *always* have a .value. No matter what. Whether checked or not. Even if you don&#039;t give a VALUE= explicitly, the system supplies one. So you aren&#039;t *really* checking anything.<BR>(3) Just doing an alert( ) of the value wouldn&#039;t check the value, of the ".checked" state, anyway.<BR><BR><BR>The better way to code this is simpler:<BR><BR>function checkProjtype( )<BR>{<BR> var cbs = document.rfsform.projtype;<BR> for ( var i = 0; i &#060; cbs.length; ++i )<BR> {<BR> // quit if you find one that is checked<BR> if ( cbs[i].checked ) return true;<BR> }<BR> alert("You must select a type of project!");<BR> return false;<BR>}<BR><BR>Note that all of this assumes you really *do* have multiple checkboxes on that form that *ALL* have the form<BR> &#060;INPUT Type=Checkbox [hl="yellow"]Name="projtype"[/hl] Value="...any..."&#062;<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts