Satch3000
Satch3000

Reputation: 49384

JQuery Add Value of 2 input boxes to a third

I have 3 input boxes in my page.

What I need to do is Onchange add the values of Input box A and Input Box B with a comma separating the two values.

For example:

Input A = 'MyValueA'
Input B = 'MyValueB' 

Result = 'MyValueA , MyValueB'

Upvotes: 1

Views: 1518

Answers (3)

Kyle Macey
Kyle Macey

Reputation: 8154

This will allow infinite textboxes

HTML

<input class="valuegroup" id="inputa" />
<input class="valuegroup" id="inputb" />

<input class="output" id="inputz" />

JS

$(function() {
  $('.valuegroup').on('change keyup', function() {
    var myVal, newVal = $.makeArray($('.valuegroup').map(function(){
        if (myVal = $(this).val()) {
            return(myVal);
        }
    })).join(', ');
    $('.output').val(newVal);

  });
});​

DEMO

Upvotes: 2

Ivan
Ivan

Reputation: 10372

$('#inputa, #inputb').change(function (e) {
  var result = $('#inputa').val() + ", " + $('#inputb').val();
  $('#inputc').val(result);
});

Upvotes: 4

Adam Shiemke
Adam Shiemke

Reputation: 3742

$("#input1, #input2").bind('change', function(){
      $("#input3").val($("#input1").val() + ',' + $("#input2").val());
});

Upvotes: 3

Related Questions