0

I need Your help with little problem with Javascript. I want to make some simple application for taxes. I've made four inputs (income, costs, social insurance, health insurance).

Math looks like:[(income - costs - social ins.) * 18% - 556,02] - health ins. (I did it little different but it should be the same).

Final sum becomes "NaN" and I don't know why :( I'll post important code below:

var message = "Your tax is ";
var income = $("#income").val();
var costs= $("#costs").val();
var social= $("#social").val();
var health= $("#health").val();

var aaa;
var bbb;
var ccc; 
var tax;

aaa = income-costs;
bbb = aaa-social;
ccc = bbb * 0.18 - 556.02;
tax = ccc-health;

And, the main page part:

<div data-role="main" class="ui-content">
    <label for="income">income:</label>
    <input type="number" name="income" id="income" value="0">

    <label for="costs">costs:</label>
    <input type="number" name="costs" id="costs" value="0">

    <label for="social">social:</label>
    <input type="number" name="social" id="social" value="0">

    <label for="health">health:</label>
    <input type="number" name="health" id="health" value="0">

    <center><a href="#" data-role="button" data-inline="true" onclick="button_clicked(null);">Check Your tax</a></center>
</div>
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
  • Try this: http://stackoverflow.com/questions/5327179/how-can-i-change-an-html-input-values-data-type-to-integer – bloodyKnuckles Apr 16 '15 at 19:01
  • If you want to stick to strictly JavaScript, you could use something like this // document.getElementById("searchTxt").value; // See here: http://stackoverflow.com/questions/11563638/javascript-get-input-text-value // Also you should state jQuery in your question if you are using that instead of JavaScript if you want your answers to reflect your question. And it doesn't hurt to add those tags either. – mcphersonjr Apr 16 '15 at 19:31

4 Answers4

0

NaN becouse undefined - undefined is … NaN, JavaScript can’t calculate value and returns Not a Number.

Your method button_clicked shoult gather values after click, not before that event.

Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
0

Thanks Guys,

It seems to be working properly now. I choose a parseInt option, it feels more convinient for me, but with getElement it looks less messy.

Sorry for post appearance, it's becouse of hurry, it wouldn't happen again.

Big thanks once more!

0

I don't why it doesn't work for you, but when I write it in JSFiddle everything seems to be OK. Btw I used $("#clickMe").click(function() { ... } function since you have jQuery.
Check it out: JSFiddle

Marin Takanov
  • 1,079
  • 3
  • 19
  • 36
0

I tried your code in JSBin and it works. Make sure you are importing JQuery:

<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>

Here is a working JSBin with the button working.

nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29