Jump to content

javascript if, then else issue.


cjkeane

Recommended Posts

Hi everyone.

 

I want to pass values to a couple of fields using javascript. the code i'm using partially works. it passes the values properly except for the fact that the payment amounts dont display properly. the 50,000 appears in the totalcreditcardlimit textbox no matter which payment method is chosen. can anyone see what i'm doing wrong? thanks.

 

<script>
function PaymentMethodCBtoTB() 
{      
if(document.getElementById("PaymentMethods").value="Cheques") { 
document.getElementById("PaymentMethods").value=document.getElementById("PaymentMethodOptions").value
document.getElementById("TotalCreditCardLimit").value="$50000.00" } 
else if (document.getElementById("PaymentMethods").value="Credit Card") { 
document.getElementById("PaymentMethods").value=document.getElementById("PaymentMethodOptions").value
document.getElementById("TotalCreditCardLimit").value=" "
document.getElementById("TotalCreditCardLimit").value="$2500.00"} 
}
</script> 

 

Link to comment
Share on other sites

the problem is the fact that you are using one '=' in your if statement which is setting the value of 'PaymentMethods' to 'Cheques'. use one '=' to set a value and two('==') for evaluation. for example:

 

foo = 'something';//set the value

if(foo == 'something') {
   //evaluated the value of foo
}

Link to comment
Share on other sites

Thanks for your reply. i have re-worked the code, but i still have an issue. The PaymentMethods textbox changes from 'Cheques' to 'Credit Card Limit' however, the 'TotalCreditCardLimit' textbox still only displays $50,000 no matter which choice I choose from the dropdown box.

 

What needs to happen is:

If cheques is chosen, the word Cheques needs to be displayed in the payment methods textbox AND the TotalCreditCardLimit textbox needs the value of $50,000

If credit card is chosen, the word Credit Card needs to be displayed in the payment methods textbox AND the TotalCreditCardLimit textbox needs the value of $2500.00

 

I'd really appreciate your help in resolving this issue.

 

<script>
function PaymentMethodCBtoTB() 
{      
VarChequesCBtoTB = (document.getElementById("PaymentMethods").value="Cheques");
VarCreditCard = (document.getElementById("PaymentMethods").value="Credit Card");
if(VarChequesCBtoTB == 'Cheques') 
{
document.getElementById("PaymentMethods").value=document.getElementById("PaymentMethodOptions").value
document.getElementById("TotalCreditCardLimit").value="$50000.00" 
} 
else if (VarCreditCard == 'Credit Card') 
{
document.getElementById("PaymentMethods").value=document.getElementById("PaymentMethodOptions").value
document.getElementById("TotalCreditCardLimit").value="$2500.00"
}
}
</script> 

Link to comment
Share on other sites

you are still setting the value of PaymentMethods to Cheques instead of comparing the actual value. Once again, '=' assigning a value and '==' is used for comparison.

 

Assigning a variable:

foo = 1;

 

Comparing the variable

if(foo == 2) {
   alert('foo is equal to two');
}

 

you have

VarChequesCBtoTB = (document.getElementById("PaymentMethods").value="Cheques");
VarCreditCard = (document.getElementById("PaymentMethods").value="Credit Card");
if(VarChequesCBtoTB == 'Cheques') {}

 

In that snippet you are assigning the value of  to PaymentMethods 'Cheques' and then again on the next line to 'Credit Card'

 

Assignment

(document.getElementById("PaymentMethods").value="Cheques");

 

Comparison

(document.getElementById("PaymentMethods").value=="Cheques");

 

In either case the variables you are assigning there would be boolean(true/false) and not a string as you expect.

 

You may want to read over the w3schools documentation on  assignment operators and javascript comparison operators

 

Here is a simplified example of your code:

pm = document.getElementById("PaymentMethods").value;
if(pm=='Cheques') {
   //your Cheques related code
} else if(pm == 'Credit Card') {
   //your Credit Card related code
}

 

Also, you may want to use proper indention(either tabs or spaces) when coding. It will make it easier to read, debug and work with in general.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.