mulitple checkbox validation(javascript) problem.

Results 1 to 2 of 2

Thread: mulitple checkbox validation(javascript) problem.

  1. #1
    Join Date
    Dec 1969

    Default mulitple checkbox validation(javascript) problem.

    I&#039m currently workin on somethin which functions like a "shopping cart". i have a search facilty and the search result displays 20 items per page.. each has its own checkbox which has the code of the item as the value.. i also have an image which says "add to cart" which will call the function below onclick.. <BR>btw, the mulitple checkbox have the same name and differs only on the value.<BR><BR>&#060;script language="javascript"&#062;<BR>var count;<BR>function check()<BR>{ <BR> count = 0; <BR> for(i=0; i&#060;window.document.frmChk.chkAdd.length; i++)<BR> {<BR> if(window.document.frmChk.chkAdd[i].checked==true)<BR> count = count + 1;<BR> }<BR> if (count == 0) <BR> {<BR> alert("Select an item to add to cart") <BR> return false<BR> } <BR> else<BR> document.frmChk.action = &#039Reservation.asp&#039<BR> window.document.frmChk.submit(); <BR> }<BR>}<BR><BR>the code works fine when i have mulitple records (therefore<BR>there are mulitple checkbox) but when my search result has only one item found (so there&#039s only one checkbox), the code doesn&#039t seem to work ..when the checkbox is checked, the value of window.document.frmChk.chkAdd.length is "undefined" instead of "1".. is there a way to fix this?? why does it work on mulitple records??<BR><BR>thanks in advance...<BR>/joe

  2. #2
    Join Date
    Dec 1969

    Default Good question, even if not ASP...

    This is just how the HTML DOM (Document Object Model) works, unfortunately.<BR><BR>When you have multiple items of the same name in a container, then an array of objects is created. If there is only one with a given name, then no array is used.<BR><BR>To make matters worse, there is no "isArray( )" function in JavaScript, so you can&#039t even easily test the type of (in your case) window.document.frmChk.chkAdd in an expression.<BR><BR>Now, you *could* check for the undefined length and use special code to handle the case of a single checkbox, but here is an idea that should work, according to the JS documentation! (Never tried it, but everything says it should work!)<BR><BR>************<BR><BR>Since like-named objects are, indeed, grouped by *name*, they do NOT have to be of the same type! <BR><BR>So add a HIDDEN form element of the same name which you will ignore! Something like <BR>&nbsp;&nbsp;&nbsp;&nbsp; &#060;INPUT TYPE=HIDDEN Name=&#039chkAdd&#039 Value=&#039&#039&#062;<BR><BR>And then a slight mod to your JS code:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;count = 0; <BR>&nbsp;&nbsp;&nbsp;&nbsp;for(i=0; i&#060;window.document.frmChk.chkAdd.length; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elem = window.document.frmChk.chkAdd[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp; if( elem.type == "checkbox" && elem.checked == true ) ++count;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>See it? By checking the type *first*, only an element named "chkAdd" that is *also* a checkbox will have its "checked" property tested! Voila.<BR><BR>Hey, if it works (or doesn&#039t work!) let us know.<BR><BR><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