Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Function

  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function

    Hi there
    I have this function that I am trying to make work and I cannot.
    The aim of the function is to pass to it both the the ID of a textbox and the contents (value) of the same textbox.
    The value of the textbox is checked against another letter and the results is displayed back into the same textbox
    The code I am using is below and when I run it I get the value of the first text for whatever I put into any of the textboxes.

    <script language="JavaScript" type="text/javascript"><!--
    function myreply(currentField)
    {
    if (currentField.value == ("y") || ("Y"))
    {
    currentField.value="Great";
    }

    else if (currentField.value == ("n") || ("N"))
    {
    currentField.value="Sorry";
    }

    else if (currentField.value == ("m") || ("M"))
    {
    currentField.value="Check back later";
    }
    }

    //--></script>
    <
    FORM name="form1">
    INPUT NAME="v1" SIZE=20 onchange="myreply(v1)" maxlength="20" >
    <INPUT NAME="v2" SIZE=20 onchange="myreply(v2)" maxlength="20" >
    <INPUT NAME="v3" SIZE=20 onchange="myreply(v3)" maxlength="20" >
    </FORM>
    Whenever I run this when I type in any of the 3 boxes I get the "Great" in that box.
    If I type "N" into v2 I get "Great" in v2 suppose to be Sorry
    "m" into v3 I get "Great" in v3 suppose to be Check back later
    "N" into v1 I get "Great" in v1 suppose to be Sorry
    any suggestions?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    try it this way
    Code:
    <script language="JavaScript" type="text/javascript"><!--
    function myreply(currentField)
    {
    if (currentField.value == "y" || currentField.value == "Y")
    {
    currentField.value="Great";
    }
    
    else if (currentField.value == "n" || currentField.value == "N")
    {
    currentField.value="Sorry";
    }
    
    else if (currentField.value == "m" || currentField.value == "M")
    {
    currentField.value="Check back later";
    }
    }
    
    //--></script>

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function

    Thank you
    It worked to perfection
    Thanks

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    To avoid those compound conditions, make the input lowercase and check for the lowercase letters.
    Code:
    function myreply(currentField)
    {
      var val = currentField.value.toLowerCase();
      if (val == "y")
      {
        currentField.value="Great";
      }
      else if (val == "n")
      {
        currentField.value="Sorry";
      }
      else if (val == "m")
      {
        currentField.value="Check back later";
      }
    }
    or even better, use switch case.
    Code:
    function myreply(currentField)
    {
      switch (currentField.value.toLowerCase())
        case "y": currentField.value="Great"; break;
        case "n": currentField.value="Sorry"; break;
        case "m": currentField.value="Check back later"; break;
        default: //do nothing, leave the value as is
      }
    }


  •  

    Posting Permissions

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