document.all question

Results 1 to 2 of 2

Thread: document.all question

  1. #1
    Join Date
    Dec 1969

    Default document.all question

    Hi,<BR><BR> It appears that when only one checkbox was "checked" on the original form, my document.all.item("checkbox").length on the submit page is invalid (normally with multiple checks it works fine). How should I first check that the item is not a list of items, but a single item?<BR><BR>Thanks,<BR>Deanna

  2. #2
    Join Date
    Dec 1969

    Default document.all doesn't work...

    ...on any browser but MSIE. Do you care?<BR><BR>A better alternative, when you are working with a form field is:<BR><BR>document.FormName.elements["checkboxName"]<BR><BR>That&#039;s universal, and goes back to version 2 or most (all?) browsers.<BR><BR>***********************<BR><BR>An yway...<BR><BR>Yes, if you have multiple form fields (of *any* kind) with the same name, then the object model gives you an *array* when you specify that name (either via document.all or via document.form.elements[name] or even via simply document.formname.checkbox). But when there is only one of a given name, you don&#039;t get an array.<BR><BR>To check to see if you have an array, you can do:<BR><BR>cb = document.formname.elements["whatever"];<BR>if ( cb.type == &#039;checkbox&#039; ) <BR>{<BR>&nbsp; &nbsp; // only one!<BR>} else<BR>&nbsp; &nbsp; // more than array<BR>}<BR><BR>But that can get to be a pain in the neck, can&#039;t it? Because now the code in the two branches is significantly different. <BR><BR>So how about a sneaky trick? *Always* add a TYPE=HIDDEN field, with the same name(!!), to the form! If you give it no VALUE (or VALUE=""), then it will never interfere with ASP processing on the next page. And yet, its existence will always force an array of fields, so your code can always use the .length.<BR><BR>If you put the hidden field first on the form, then it will always be element 0 of the array, so you could just then start processing at element 1 of the array! presto.<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