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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in IF statement 1

Status
Not open for further replies.

TheCandyman

Technical User
Sep 9, 2002
761
US
I'm getting a validation error in this small function. The problem is the fourth line down on the '&&' which i tried to bold.

Code:
function tabOnEnter (field, evt) {
		 var keyCode = document.layers ? evt.which : document.all ?
		 evt.keyCode : evt.keyCode;
		   if (keyCode != 13 [b]&&[/b] keyCode !=9)
			 return true;
		   else {
			 var el=getNextElement(field);
			 if (el.type!='hidden')
				el.focus();
			 else
				while (el.type=='hidden')
				   el=getNextElement(el);
				el.focus();
			 return false;
		   }
		 }

What's wrong with that?
 
I believe that it has something to do with the way && works. See which implies that you should brace off each of the tests inside the main braces to force each to return a boolean value.
Code:
 if ([COLOR=red]([/color]keyCode != 13[COLOR=red])[/color] && [COLOR=red]([/color]keyCode !=9[COLOR=red])[/color])

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Hi

johnwm said:
you should brace off each of the tests inside the main braces
Sorry, but that is wrong. Operator Precedence in MDC ( or Operator Precedence (JavaScript) in MSDN if you prefer it ) clearly show that [tt]!=[/tt] has higher priority than [tt]&&[/tt], so they will be evaluated first without the parenthesis too.
TheCandyman said:
I'm getting a validation error
From that I understand that the error is in HTML, not in JavaScript. More precisely, in XHTML. According to W3C Markup Validation Service, in XHTML the ampersand has to be written as character entity, even between [tt]script[/tt] tags. ( No error for them in HTML 4.01 or HTML 5. )

So you can either
[ul]
[li]change your DOCTYPE to HMTL 5 ( development of XHTML was stopped anyway )[/li]
[li]use character entities :
Code:
[b]if[/b] [teal]([/teal]keyCode [teal]!=[/teal] [purple]13[/purple] [teal]&[/teal]amp[teal];&[/teal]amp[teal];[/teal] keyCode [teal]!=[/teal][purple]9[/purple][teal])[/teal]
[/li]
[li]enclose the script in CDATA :
Code:
[teal]<[/teal]script type[teal]=[/teal][green][i]"text/javascript"[/i][/green][teal]>[/teal]
[teal]<![[/teal]CDATA[teal][[/teal]
[gray]// JavaScript code here[/gray]
[teal]]]>[/teal]
[teal]</[/teal]script[teal]>[/teal]
[/li]
[/ul]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top