Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm having trouble using the Logical Operator && 1

Status
Not open for further replies.

LyndonOHRC

Programmer
Joined
Sep 8, 2005
Messages
603
Location
US
The first code block works fine, however, I have three conditions for my if statement. I am new at this and understand that the && logical operator is the same as the OR logical operator in other languages.

The second code section is how I'm trying to use multiple conditions in this if statement but it always evaluates to false even when I know the data should return a true.

Any help is appreciated.
Lyndon

This works:
Code:
function FillAmount()
   	{
		if (document.forms["ReceiptForm"].elements["LicenseTypeOther"].options[document.forms["ReceiptForm"].elements["LicenseTypeOther"].selectedIndex].value=='Blacksmith')
		{
			if (document.ReceiptForm.Amount.value!=='120')
			{
	    	   	document.forms["ReceiptForm"].elements["Amount"].value = document.forms["ReceiptForm"].elements["LicenseTypeOther"].options[document.forms["ReceiptForm"].elements["LicenseTypeOther"].selectedIndex].title;
			}
		}
		else
		{
			document.forms["ReceiptForm"].elements["Amount"].value = document.forms["ReceiptForm"].elements["LicenseTypeOther"].options[document.forms["ReceiptForm"].elements["LicenseTypeOther"].selectedIndex].title;
		}
    }

This does not, it always evaluates to false:

Code:
function FillAmount()
   	{
		if (document.forms["ReceiptForm"].elements["LicenseTypeOther"].options[document.forms["ReceiptForm"].elements["LicenseTypeOther"].selectedIndex].value=='Blacksmith' [COLOR=red]&& [/color red]document.forms["ReceiptForm"].elements["LicenseTypeOther"].options[document.forms["ReceiptForm"].elements["LicenseTypeOther"].selectedIndex].value=='Veterinarian')
		{
			if (document.ReceiptForm.Amount.value!=='120')
			{
	    	   	document.forms["ReceiptForm"].elements["Amount"].value = document.forms["ReceiptForm"].elements["LicenseTypeOther"].options[document.forms["ReceiptForm"].elements["LicenseTypeOther"].selectedIndex].title;
			}
		}
		else
		{
			document.forms["ReceiptForm"].elements["Amount"].value = document.forms["ReceiptForm"].elements["LicenseTypeOther"].options[document.forms["ReceiptForm"].elements["LicenseTypeOther"].selectedIndex].title;
		}
    }
 
That always evaluates to false because you can't possibly have 2 different values for the same form element. Are you sure you you aren't looking for the logical or operator? (||)

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Also, just a hunch.

By any chance, this isn't a "multiple" select dropdown where you can select more than one option, is it? If that's the case you have to loop thru the selected options and check them all individually.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
It's not a multiple select box.

What I'm trying to do is:

If the select is 'Veterinarian' or select 'Blacksmith'
If Amount.Value not equal to '120'
Amount.value=select.title
Else
do nothing
else
Amount.value=select.title
end
 
See my above post:
Are you sure you you aren't looking for the logical or operator? (||)

You don't want &&, that's the [!]and[/!] operator.

You want ||, it's the [!]or[/!] operator.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Maybe I have the syntax wrong. I thought: && is logical OR and || is Logical AND.

I feel bad asking such a basic question but I have read basic javascript tutorials and haven't found one yet that really discusses using AND and OR. An of course you can't search for these two terms because they are reserved words in the search engine.

Maybe someone could suggest a tutorial link...
 
This link found from googling "javascript logical operators"


-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Thanks kaht!

The turorial I read had an operator chart with them backwards. I'll email the author.

Lyndon
 
The turorial I read had an operator chart with them backwards.quote]

That ought to be a hanging offense!

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top