For this function, I am trying to calculate the outcome of the $r variables and display them in a form input text field, "otherHtmlIdName".
The if statement checks to see if they all have values, then gets the calculated variable's value and changes the input field. It works great, the problem is that it works 4 times in a row, once for every parseInt()
.
Is there any way I can just make it run once? I've tried parseFloat
and Number
as well. Didn't help.
[Edit]: This wasn't happening because of parseInt. I was calling the click function on this ID 4 times in my code.
$("#htmlIdName").click(function () {
var variableName = parseInt($r.ab3, 10) - parseInt($r.ab4, 10) + parseInt($r.sb3, 10) + parseInt($r.sb4, 10);
if ($r.ab3 && $r.ab4 && $r.sb3 && $r.sb4) {
$("#otherHtmlIdName").val(variableName);
$("#otherHtmlIdName").change();
};
});
Try:
$("#htmlIdName").off().on('click', function() {
/* Your code here */
});