I have the following code. I want to add two input fields together and output it to the page. I get as far as being able to output what is type in the input field however I can’t figure how to add the two fields together and output it. I have it at http://jsfiddle.net/erick/9Dj3j/3/
Jquery
$(function() {
var output_element = $('#output_ele');
var output_element1 = $('#output_ele1');
$('#the_input_id').keyup(function() {
var their_input = $(this).val();
var firstInput = output_element.text(their_input);
});
$('#the_input_id1').keyup(function() {
var their_input1 = $(this).val();
var firstInput1 = output_element1.text(their_input1);
});
var output_total = $('#total');
var total = firstInput + firstInput1;
output_total.text(total);
});
HTML
<form method="post">
<input type="text" id="the_input_id">
<input type="text" id="the_input_id1">
</form>
<div id="output_ele">
</div>
<div id="output_ele1">
</div>
<div id="total">
</div>
The code you posted isn’t working for a couple of reasons
keyUpyou assign the new values tofirstInputandfirstInput1which are local to the callbackundefined)What you need to do is write a function that adds the two values together and call that from the
keyUphandlerFiddle: http://jsfiddle.net/9Dj3j/56/