...

View Full Version : Function



devron
03-15-2005, 08:42 PM
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?

_Aerospace_Eng_
03-15-2005, 09:30 PM
try it this way

<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>

devron
03-15-2005, 10:32 PM
Thank you
It worked to perfection
Thanks

glenngv
03-16-2005, 07:14 AM
To avoid those compound conditions, make the input lowercase and check for the lowercase letters.

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.

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
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum