Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-15-2005, 07:42 PM   PM User | #1
devron
New to the CF scene

 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
devron is an unknown quantity at this point
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.

Quote:
<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>
<
Quote:
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?
devron is offline   Reply With Quote
Old 03-15-2005, 08:30 PM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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>
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-15-2005, 09:32 PM   PM User | #3
devron
New to the CF scene

 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
devron is an unknown quantity at this point
Function

Thank you
It worked to perfection
Thanks
devron is offline   Reply With Quote
Old 03-16-2005, 06:14 AM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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
  }
}
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:05 AM.


Advertisement
Log in to turn off these ads.