Jonny Jordan
Jonny Jordan

Reputation: 407

Use jQuery to combine values of two inputs in a third input

I'm just learning jQuery and I'm trying to take the two values from boxes one and two and add them together into another input separated by ||.

For example if I add "hello" into the first text box and "you" in the second box I would like to display hello||you in the third box.

This is what I have so far: https://fiddle.jshell.net/a2n234eq/

Upvotes: 0

Views: 3072

Answers (2)

Joerg
Joerg

Reputation: 3101

$("#1, #2").on("keyup", function(){
    $("#3").val($("#1").val() + "||" + $("#2").val());
});

Upvotes: 5

Mike Donkers
Mike Donkers

Reputation: 3699

If I understood correctly you want it like this.

What I've done here is I've taken your code, and changed it so the value get's stored in a variable which is put into the third input when the last input has changed.

The code looks like this:

HTML

<input id="1">
<input id="2">
<input id="3">

JS

var value;
$('#1').change(function() {
    value = $(this).val();
});
$('#2').change(function() {
    value += "||"+$(this).val();
    $('#3').val(value);
});

Don't forget to include jQuery in the HTML

Hope this helps!

Upvotes: 0

Related Questions