PDA

View Full Version : get the value from one function and use it in another



newscript
Mar 5th, 2011, 03:47 AM
Hi all,

I have created two sets of radio buttons. One for the tour type, the other for the discount type.

I want to get the value from the tour function and put it into the discount formula in the payment function. I tried to write the code for the discount formula in the payment function but it does not work.

Thanks in advance.:confused:

Partial codes are as follows:

function selectedTourTypeCost()
{
var radioButtonTour; //A tour type radio button.
var costTour = 0; //Cost of the selected tour type.

//Get the number of the selected tour (1,2,3 or 4)
for (var i = 1; i <= 4; i++)
{
radioButtonTour = document.getElementById("tour" + i);
if (radioButtonTour.checked == true)
{
costTour = parseInt(radioButtonTour.value);
}//end if loop
}//end for loop of selected tour

return costTour;
}//end function SelectedTourTypeCost()



function selectedPaymentType()
{

var radioButtonPayment;//A payment type radio button
var selectedPayment = 0; //The number of the selected payment.

//Get the number of the selected payment (1,2,3 or 4)
for (var i = 1; i <= 4; i++)
{
radioButtonPayment = document.getElementById("payment" + i);
if (radioButtonPayment.checked == true)
{
selectedPayment = i;

}//end if loop for getting the selected payment number
}//end for loop for getting the selected payment type


//Calculate the appropriate response
if (selectedPayment == 1)
{

var discount = 0;
discount = (10/100) * selectedTourTypeCost();
}

alert ("Discount:" + discount);


}//end selectedPaymentType() function



</script>
</head>

Philip M
Mar 5th, 2011, 07:52 AM
Place the declaration
var costTour = 0; //Cost of the selected tour type.
outside the functions to make it global (that is useable by another function).
Then
var discount = 0;
discount = (10/100) * costTour; // i.e. discount 10%

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

bullant
Mar 5th, 2011, 09:01 AM
It's not clear from your code when these functions are actully called but if selectedPaymentType() is called immediately after selectedTourTypeCost(), rather than use globals, which can lead to problems if another function accidentally uses a global variable when it's not supposed to, you could just call the second function as the last step in the first function and pass the value you need to the second function.



function selectedTourTypeCost()
{
var radioButtonTour; //A tour type radio button.
var costTour = 0; //Cost of the selected tour type.
//Get the number of the selected tour (1,2,3 or 4)
for (var i = 1; i <= 4; i++)
{
radioButtonTour = document.getElementById("tour" + i);
if (radioButtonTour.checked == true)
{
costTour = parseInt(radioButtonTour.value);
}//end if loop
}//end for loop of selected tour

selectedPaymentType(costTour);

}//end function SelectedTourTypeCost()


function selectedPaymentType(cost) {
...
...
discount = (10/100) * cost;
...



but you are already calling selectedTourTypeCost() in this line discount = (10/100) * selectedTourTypeCost(); but obviously this isn't working hence your post.

It would help if you show where and when the 2 functions are actually called in your code for the best solution.

newscript
Mar 7th, 2011, 10:59 PM
Thank you for all your help.