-1

I have a form field, x_amount, being populated with a static number, based on a selection from a drop down, which for some reason came to me as x_ship_to_address. If 1 or 2 are selected, x_amount is poulated with 25 or 45. If 3 or 4 are selected, the user enters a value into payment_mount and then x_amount becomes payment_amount x 1.03. I want, if a user selects 1 or 2, then both payment_amount and x_amount are populated with 25 or 45. Here is the JS that is working to populate x_amount with a static number:

function SI_money(amount) {
    // makes sure that there is a 0 in the ones column when appropriate
    // and rounds for to account for poor Netscape behaviors 
    amount=(Math.round(amount*100))/100;
    return (amount==Math.floor(amount))?amount+'.00':((amount*10==Math.floor(amount*10))?amount+'0':amount);
}

function calcTotal(){
var total = document.getElementById('x_amount');
var amount = document.getElementById('payment_amount');
var payment = document.getElementById('x_ship_to_address');

if( payment.selectedIndex == 0)
    total.value = 'select Type of Payment from dropdown';
else if( payment.selectedIndex == 3 || payment.selectedIndex == 4 )
    total.value = SI_money(parseFloat(amount.value * 1.03));
else if( payment.selectedIndex == 1 )
    total.value && amount.value = SI_money(25.00);
else if( payment.selectedIndex == 2 )
    total.value = SI_money(45.00);
}

I think I want the last two ifs of calcTotal to be something like:

else if( payment.selectedIndex == 1 )
    total.value && amount.value = SI_money(25.00);
else if( payment.selectedIndex == 2 )
    total.value && amount.value = SI_money(45.00);

But adding the && throw the errors. I think I'm just missing something about the syntax - how do I say both fields get populated with the correct static number?

Lauren
  • 47
  • 2
  • 7

1 Answers1

1

&& doesn't mean "do this AND that". You need to execute these separately:

total.value && amount.value = SI_money(25.00); <-- wrong

Correct:

total.value = SI_money(25.00);
amount.value = SI_money(25.00);

Also you really need to read this: Code Conventions for the JavaScript Programming Language. There is a suspicious absense of curly brackets in your code.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Great - that was a simple fix. And thanks for the link, I have added curly brackets around each statement. – Lauren Apr 02 '13 at 19:36
  • @Diodeus What about total.value = amount.value = SI_money(25.00); ? Anything like bad practice in this (Though I have never written this way in the past) – hop Apr 02 '13 at 19:38
  • You can do that, but you cannot scope the variables along the way, so I usually avoid id. See: http://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript – Diodeus - James MacFarlane Apr 02 '13 at 19:40