Weird Calculations

Weird Calculations

by Anis Jradah -
Number of replies: 5
Dear All,

Kindly, I am using the below javascript for a simple calculations of subtraction, but it is giving a weird numbers,
for example 3-2=3

<script type="text/javascript">
var capacity = 0;
var taken = 0;


document.getElementById('Capacity#id').onkeypress=function(){setRemainingC();};
document.getElementById('Taken#id').onkeypress=function(){setRemainingCapacity();};
function setRemainingCapacity(){
var capacity=document.getElementById('Capacity#id').value;
var taken=document.getElementById('Taken#id').value;

document.getElementById('Remainingcapacity#id').value=Number(capacity)-Number(taken);

}
</script>

Please advise on how to solve this problem and thank you for your time.

Best regards,
Anis
Average of ratings: -
In reply to Anis Jradah

Re: Weird Calculations

by William Lu -
Picture of Particularly helpful Moodlers
Please attach your preset, so others can help you to fix it.
In reply to William Lu

Re: Weird Calculations

by Anis Jradah -

Dear William,


Thank you for your reply.

Kindly, I have attached the preset.


Best regards,

Anis

In reply to Anis Jradah

Re: Weird Calculations

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Anis,

If I am not mistaken, all you need to do is in your code where you have, onkeypress, in two places, is to change it to, onchange. The math then seems to work correctly for me, with all the samples I tried.

Average of ratings: Useful (2)
In reply to AL Rachels

Re: Weird Calculations

by Anis Jradah -

Dear AL,

Thank you for the help. I have tested it and it is now working perfectly.


Best regards,

Anis