Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 20th, 2004, 05:53 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default Is one (1) a 'length' for option buttons?

Hi:

I have the following need: based on an email address entered into a text box, a table is created with contact information for records that match the email address, one per row.

The last row will always have a 'Create new contact'. In the case of no matching contact records, it will be the only row. Option buttons are included with each row in order to identify the selection. Depending on the selection, different forms are opened when the form is submitted. So far, no problems with any of this.

I'm concerned that after a group of records is brought down, that the user will edit the email address. If that happens, especially if a selection is made, then the action of the submit button is ambiguous. So, I've been trying to use the onchange() event to

1) determine if a selection was made and, if so, unset it
2) hide (or eliminate) the table so it is obvious to the user what the submit button should do

This works for the most part with the following exception: when there are no matching records, the only possible selection is 'Add new contact'. If I check that, then change the email address, the table does not hide, and the option button does not reset, BUT if I click submit, it works as intended: it gets any matching contact records for the new email address rather than the predefined page.

Below are code snippets of the HTML and javascript.

Here is the only clue I have:

When contact records that match the email address are found, then the option group has a length in the javascript.

When no contact records are found and only the 'New contact record' option button displays, the length propery diplays 'undefined'.

I don't understand that. Isn't one (1) a length?

Thanks,
JK

Here is the page source when matching contacts are found:
================================================== =======

<div id=divContactSelections>
<Table Border=1 id=tblContactSelections>

<TR>
<TD class='copy'><INPUT Type=radio Name=radContactID Value=C6UJ9A003RSB onclick=setTargetPageID(40)></TD>
<TD class='copy'>Juan Rey</TD>
</TR>

<TR>
<TD class='copy'><INPUT Type=radio Name=radContactID Value=C6UJ9A002H6E onclick=setTargetPageID(40)></TD>
<TD class='copy'>John King</TD>
</TR>

<TR>
<TD class='copy'><INPUT Type=radio Name=radContactID Value=NewContact onclick=setTargetPageID(30)></TD>
<TD class='copy'>Create New Contact</TD>
</TR>

</Table>
</div>

Here is the page source when no matching contacts are found:
================================================== ==========
<div id=divContactSelections>
<Table Border=1 id=tblContactSelections>

<TR>
<TD class='copy'><INPUT Type=radio Name=radContactID Value=NewContact onclick=setTargetPageID(30)></TD>
<TD class='copy'>Create New Contact</TD>
</TR>

</Table>
</div>



Here is the javascript function I'm using for the onchange event for the text box:
================================================== ================================

function checkSelection()

{

    var blnHideContactSelections = false; //initialize flag

    if (document.frmCustomerEmail.radContactID != null) // determine if option buttons exist yet
                                // initial load of page vs subsequent load

        {

        var ctlGroup = document.frmCustomerEmail.radContactID; // option button name
        var intNumberOfControls = ctlGroup.length; // identify how many options are available

        alert(intNumberOfControls);

        var element;
        var controlIndex;

        for (controlIndex=0; controlIndex < intNumberOfControls; controlIndex++) // loop through
            {

            element = ctlGroup[controlIndex];
            if (element.checked == true) // if the item is checked
                {
                element.checked = false // uncheck it
                blnHideContactSelections = true // set flag
                break; // exit for
                }
            }

        }

        if (blnHideContactSelections == true)
            {
            document.getElementById("tblContactSelections").st yle.visibility = "hidden"; // hide the table
            document.getElementById("tblContactSelections").st yle.display = "none"; // recover the canvas
            }
}
 
Old November 4th, 2004, 04:31 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii John K. King
When there is only one Radio Button ,in that case ctlGroup.length gives "undefined" ,
Since ctlGroup.length expecting that ctlGroup is an array of objects.
So it's better to check "undefined" ,if not then go through the loop.
Cheers!!!


vinod
 
Old November 7th, 2004, 06:04 PM
Authorized User
 
Join Date: Jun 2003
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Vinod:

Thank you for the information.

I've found that technique useful before, but never thought to apply it in this case. I worked around it by having a second radio button, but not displaying it by setting the visible and display attributes.

Testing for 'undefined' is a better solution.

I appreciate your help.

JK





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Check Boxes/Option buttons/Option Group hewstone999 Access VBA 1 March 14th, 2008 07:25 AM
Error is using option buttons return value Subuana Beginning VB 6 2 May 1st, 2006 03:39 PM
Using Option buttons NovieProgrammer Access VBA 1 April 2nd, 2005 09:16 PM
Option buttons, queries and combo boxes... Anubis Access VBA 26 November 20th, 2003 12:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.