cjkeane Posted March 19, 2011 Share Posted March 19, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/231072-javascript-if-then-else-issue/ Share on other sites More sharing options...
tomfmason Posted March 19, 2011 Share Posted March 19, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/231072-javascript-if-then-else-issue/#findComment-1189508 Share on other sites More sharing options...
cjkeane Posted March 19, 2011 Author Share Posted March 19, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/231072-javascript-if-then-else-issue/#findComment-1189552 Share on other sites More sharing options...
tomfmason Posted March 19, 2011 Share Posted March 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231072-javascript-if-then-else-issue/#findComment-1189590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.