0

Possible Duplicate:
Addition is not working in JavaScript

In an attempt to learn some java script, I'm trying to build a simple system that adds values when buttons are clicked..

The following code works, but will only add the hardcoded value of +100, can anyone help me change this code so it adds the value nested in the id="add" button?

<html>
  <head>
        <title>Input tutorial</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script language="javascript">
               $(function () {
    $("#add").click(function() {
    $("#total").val(parseInt($("#total").val()) + 100)
});
});
        </script>
  </head>
  <body>
     <input type="button" value="12" id="add">
     <input type="button" value="14" id="total">

  </body>
</html>

Any help would be great! Thank you.

Community
  • 1
  • 1
user1789437
  • 490
  • 1
  • 7
  • 22

2 Answers2

1

Just replace the hardcoded value of 100 by the value you want:

$("#total").val(parseInt($("#total").val()) + parseInt($("#add").val()) )
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
0
<html>
<head>
    <title>Input tutorial</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script language="javascript">
        window.onload = function() {
            $('#add').bind('click', function () {
                $("#total").val(parseInt($("#total").val()) + parseInt($("#add").val()) + 100);
        });
    }
    </script>
</head>
<body>
<input type="button" value="12" id="add">
<input type="button" value="0" id="total">
</body>
</html>
mavili
  • 3,385
  • 4
  • 30
  • 46