user3224487
user3224487

Reputation: 23

Store Javascript element value calculation in variable

I would like to store the result of a calculation done in a Javascript function in a variable to be used for later, any way to do this?

This calculation is performed to show the user the sum of his/her selection in an HTML form:

<script type="text/javascript">
function updateCDiskInput(val) {
document.getElementById('CDiskInput').value=val*150;
}
</script>

Can the result of this calculation be stored in a variable for later use?

Upvotes: 1

Views: 1751

Answers (1)

Fenton
Fenton

Reputation: 251102

You can put the result in a variable:

function updateCDiskInput(val) {
    return document.getElementById('CDiskInput').value=val*150;
}

var result = updateCDiskInput(1);

You could update a variable from within the function, but that is more volatile as your code will be temporally coupled (when you use the value, you would have to know if the function had run or not).

var result;

function updateCDiskInput(val) {
    result = document.getElementById('CDiskInput').value=val*150;
}

Upvotes: 1

Related Questions