Reputation: 163
How to add my textbox 1 and 2 value and pass to textbox 3?
<input name="1" id="1" value="" >
<input name="2" id="2" value="" >
<input name="3" id="3" value="" readonly>
Here's my fiddle http://jsfiddle.net/Zy46N/6/
Upvotes: 0
Views: 26884
Reputation: 57105
Adding Two Strings with space separated.
var input = $('[name="1"],[name="2"]'),
input1 = $('[name="1"]'),
input2 = $('[name="2"]'),
input3 = $('[name="3"]');
input.change(function () {
input3.val(input1.val() + ' ' + input2.val());
});
if It's not a valid Number take it's value 0
var input = $('[name="1"],[name="2"]'),
input1 = $('[name="1"]'),
input2 = $('[name="2"]'),
input3 = $('[name="3"]');
input.change(function () {
var val1 = (isNaN(parseInt(input1.val()))) ? 0 : parseInt(input1.val());
var val2 = (isNaN(parseInt(input2.val()))) ? 0 : parseInt(input2.val());
input3.val(val1 + val2);
});
Upvotes: 5
Reputation: 703
Try this
$('input').change(function() {
if($('[name="1"]').val()!=="" && $('[name="2"]').val()!=="")
{
$('[name="3"]').val(parseInt($("#1").val())+(parseInt($("#2").val())));
}
else
{
$('[name="3"]').val("");
}
});
Upvotes: 1
Reputation: 1296
your code itself is working and just need a little of changing.
$('[name="1"], [name="2"]').change(function() {
$('[name="3"]').val($('#1').val() + $('#2').val());
});
Fiddle sample.
Upvotes: 0
Reputation: 3645
You can use val and change function from jquery
$("#1, #2").change(function(){
var val1 = $("#1").val(),
val2 = $("#2").val();
$("#3").val(val1 + val2);
});
Upvotes: 1
Reputation: 148110
Use class selector
for textboxes
to add, use alph numeric ids
. Use parseFloat
to convert text
to number
.
$('.common').change(function () {
$('#id3').val(parseFloat("0"+$('#id1').val()) + parseFloat("0"+$('#id2').val()));
});
Upvotes: 4
Reputation: 15387
Try this
$('input').change(function() {
$('[name="3"]').val(parseInt($("#1").val())+(parseInt($("#2").val())));
});
Upvotes: 1
Reputation: 5947
Use this in your javascript function.
var sum=$("#txtbox1").val()+$("#txtbox2").val();
// Assign sum to third textbox
$("#txtbox3").val(sum);
Upvotes: 1