Forum Moderators: open
Thanks -
Code:
function writeChange_onclick(checkBoxSelected) {
switch (checkBoxSelected) {
case 'DVD-ROM':
var writePrice = 59;
break;
case 'CD-ROM':
var writePrice = 29;
break;
default:
var writePrice = 129;
break;
}
var writeTotal;
writeTotal = writeTotal+ '$' +writePrice; //**this is where I have a problem
var txtBox = document.form1.txtFeedBack;
var priceBox = document.form1.priceFeedBack;
var writeDesc = txtBox.value; //name value of checkbox - but this actually just seems to declare variable
var priceWrite = priceBox.value;
writeDesc = writeDesc + checkBoxSelected; //add the name value of the check box each time
writeDesc = writeDesc + ' $'+writePrice+ '\n'; //add price based on switch statement along with return
txtBox.value = writeDesc; //write value back to textbox
priceBox.value = priceWrite + writeTotal; //write price total back to (price) textbox
//end of function
}
The second problem is scope, I assume you mean writeTotal to be added to each time the function is called, so writeTotal needs to exist between calls, that is to be a global variable.
var writeTotal = 0;
function writeChange_onclick(checkBoxSelected) {
...
writeTotal = writeTotal + writePrice; // or writeTotal += writePrice;
...
}
You will also need to consider what happens if item unselected.
I never really understood why even without the '$' string e.g. two numbers - the variable writeTotal fed back as undefined? I thought js was smart about variables?
var writeTotal = writeTotal + writePrice;
would to me be set as numerical because of writePrice or perhaps there is someway to declare writeTotal - I tried parseInt() but was not greeted with success. Anyway - many thanks again...
Sam.