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.

0

2 Answers 2

1

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

$("#total").val(parseInt($("#total").val()) + parseInt($("#add").val()) ) 
Sign up to request clarification or add additional context in comments.

Comments

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> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.