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

Problem with If Else Statement

Status
Not open for further replies.

mych

Programmer
Joined
May 20, 2004
Messages
248
Location
GB
I have the following code that is triggered by a button on a form...

Code:
function ComposeGMQ()
	{
	var PQ = document.getElementById('PtQ').value
	alert("PQ=" + PQ) [red]This will always show the value of PtQ [/red]
	
	var DPC= document.getElementById('DstPcode').value
	DPC = DPC.toUpperCase()
	DPC.replace(" ", "+")
	
	var GMQ= ""
	
	if (PQ = 3)
		{
		  alert("PQ3") [red]No matter what the value of PQ which should be the same as PtQ this alert always triggers [/red]
			Some extra code....
		}
		else if (PQ = 2)
		{
		  alert("PQ2")
			Some other Code....
		}
		else 
		{
		  alert("PQ1")
		  	Even more Code
		}
	
	win = window.open(GMQ, "js");

The element PtQ can have a value of 1, 2 or 3

I placed the Alerts to see where the code goes wrong.

The problem I have is that no matter what the value of PtQ the first Alert box will show the correct value of PtQ but the if else statement will always trigger the Alert("PQ3") which should only trigger if the value of PtQ is 3

Any assistance is figuring this one out would be appreciated.

Regards
Mych
 
Why can't languages be more alike especially in mathamatical comparisons... I'm so use to VB that I make silly mistakes in JS. Thanks Jeff
 
Why can't languages be more alike especially in mathamatical comparisons...

They are - C, C++, Java, JavaScript - they all use "==" for equality testing. VB is the odd-one out in that group.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi

Dan said:
They are - C, C++, Java, JavaScript - they all use "==" for equality testing. VB is the odd-one out in that group.
I agree. And I would like to mention a few more languages which also use == for equality test : AWK, BC, Perl, PHP, Python, Ruby, TCL.

Feherke.
 
Way to remember

== "is equal to" or "is equal 2"

There is a logical difference between = and ==

I make that error more often than I'd like to. But now it's one of the first things I check when debugging.

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top