JavaScript calculation won't print results

parseint javascript
write a javascript program to calculate multiplication and division of two numbers (input from user)
arithmetic operations in javascript examples
javascript operators
javascript math formulas
javascript calculator
javascript math operators
modulo javascript

I have this code

$(function() {
  if(document.getElementById('price') !== null && document.getElementById('dp') !== null){
    var price = document.getElementById('price').value;
    var deposite = document.getElementById('dp').value;
    document.getElementById('remained').value = parseInt(price)-parseInt(deposite);
  }
});

and this fields in my form

<div class="col-md-3">
    <label for="price">Price *</label>
    <input type="number" class="form-control" id="price" name="price">
</div>
<div class="col-md-3">
    <label for="dp">DP *</label>
    <input type="number" class="form-control" id="dp" name="dp">
</div>
<div class="col-md-3">
    <label for="remained">Remained *</label>
    <input type="number" class="form-control" id="remained" name="remained">
</div>

The logic is simple:

  1. get price
  2. get DP
  3. print minus results in remained input

but somehow it doesn't print anything in remained input.

Any idea what I did wrong?

Your code is executing on page load and the value of the inputs are empty.

You should execute your code on some event like the following way:

$(function() {
  $('#dp, #price').on('input', function(){    
    if(document.getElementById('price') !== null && document.getElementById('dp') !== null){
      var price = document.getElementById('price').value;
      var deposite = document.getElementById('dp').value;
      document.getElementById('remained').value = parseInt(price)-parseInt(deposite);
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-3">
    <label for="price">Price *</label>
    <input type="number" class="form-control" id="price" name="price">
</div>
<div class="col-md-3">
    <label for="dp">DP *</label>
    <input type="number" class="form-control" id="dp" name="dp">
</div>
<div class="col-md-3">
    <label for="remained">Remained *</label>
    <input type="number" class="form-control" id="remained" name="remained">
</div>

Basic math in JavaScript, calculating new values, and so on, that you won't be surprised to learn that JavaScript has a full-featured set of math functions available. I'm making a form in which I want to automate some calculations. The form contains a table with some inputs. The Javascript is below the form. For a reason unknown to me, the calculation won't start or isn't performed correctly. Here's the code:

I think you should put the calculation to ready function because it is asynchronous

 $( document ).ready(function() {
     console.log( "ready!" );
     // calculation
  });

Or better use angular ... Instead of jQuery

Beginning JavaScript, After all, the more calculations you force JavaScript to do, the slower it will be, though admittedly just one calculation won't tax it too much. Another reason for  In this tutorial, we will learn how to perform calculations using JavaScript. It is quite often required to do calculations online in order forms, request quote forms and the like. This tutorial will show you how to use different form elements like drop-down list, radio button, check box and text box in a calculation.

How I taught Katy Perry (and others) to program in JavaScript, After all, the more calculations you force JavaScript to do, the slower it will be, though admittedly just one calculation won't tax it too much. Another reason for  Calculation Fields Not Updating. I have several fields that update the total price "live" but I have one field that went a number is entered, it updates the sum of it's field, but it will not update my "total" field until I change the checkbox. Field 1: - Works perfectly var a = this.getField("Black.Qty"); var b = this.getField("Natural.Qty");

The js code runs on the initial loading of webpage. The js code must be called by an onclick or an onkeyup

JavaScript basic: Calculate number of days left until next Christmas , "Absolutely right Taylor," I said, "the calculate() methods of those classes simply won't work with our GUI. We need a calculate() method that uses the values the  This calculator is awesome and I just want to change one thing. I've removed the readonly property from .display so I can enter numbers and operator symbols, but I want to be able to calculate by hitting Enter. I don't know JavaScript well, but this is what I have so far:

Calculation - JavaScript, JavaScript exercises, practice and solution: Write a JavaScript Write a JavaScript program to calculate number of days left until next Christmas. getFullYear() won't help our countdown for next year until nextYear/0/1. I have created a budget worksheet to be used with a grant application. The cells in the table are set to auto calculate to help the user find the totals and check to make sure their funds requested match their funds matched. I have checked the field calculation order and still nothing works. I have

Build a Countdown Timer in Just 18 Lines of JavaScript, Okay this is my first try at doing some calculations using javascriptand I think you will However, it won't increase the number of TOTAL Calories that you burn . Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.

JavaScript Arithmetic, Yaphi Berhanu creates a JavaScript clock widget to demonstrate how coding your own You won't need to load external scripts and stylesheets. We then calculate the difference between that time and the current time. Predefined calculations ; Simplified field notation ; Custom JavaScript calculations ; In reality, all three are JavaScript, but for the first two, Acrobat builds the JavaScript code for us. Calculations are only available for text fields and are accessed through the Text Field Properties dialog. To enter a calculation: Activate the Select Object Tool.

Comments
  • The fields aren't filled on pageload, so won't it always print 0? Or does it not print anything at all into the #remained?
  • @CertainPerformance doesn't print anything at all, i also tried to print static text in console.log() didn't print as well
  • parseInt returns NaN when passed the empty string, and NaN can't be set to the value of a numeric input field. Don't know why you're trying to retrieve the values on pageload though, that doesn't make sense - they're empty
  • in 6min will be able to accept the answer please be patient :)
  • that's the idea to have button but unfortunately it wasn't my interest for this case, and already fixed, thanks for the idea
  • thanks @mafortis , if you need any further help tag me 😊