jeff
jeff

Reputation: 15

How to transfer input value from one input to another with jquery

I need to be able to type in to an input and have that value show up in another input here is what I have been working with. So basically input from userreplymessage goes to input userreplydisplay.

html

<form id="userreply">
    <select id="usernames">
        <option value="">Users</option>
        <option value="Alex">Alex</option>
        <option value="Jeff">Jeff</option>
        <option value="Amy">Amy</option>
        <option value="Kate">Kate</option>
    </select>
    <input id="userreplymessage" type="text" />
    <input id="userreplydisplay" type="text" />
</form>​

Jquery

$(document).ready(function() {
    function update() {
        $('#userreplydisplay').text('startreply ' + 'middlereply ' + $('#usernames').val() + ' endreply ' + $('#userreplymessage').val());
    }

    $('#userreplymessage').keyup(update);
    $('#usernames').change(update);
});​

Upvotes: 1

Views: 2077

Answers (3)

Guffa
Guffa

Reputation: 700192

Use the val method to set the value of the input, not the text method:

$('#userreplydisplay').val('startreply ' + 'middlereply ' + $('#usernames').val() + ' endreply ' + $('#userreplymessage').val());

Demo: http://jsfiddle.net/8rxsg/

Upvotes: 1

tusar
tusar

Reputation: 3424

This Line uses text() :

$('#userreplydisplay').text('startreply ' + 'middlereply ' + $('#usernames').val() + ' endreply ' + $('#userreplymessage').val());

It should be val()

$('#userreplydisplay').val('startreply ' + 'middlereply ' + $('#usernames').val() + ' endreply ' + $('#userreplymessage').val());

Upvotes: 3

jamesmortensen
jamesmortensen

Reputation: 34038

Both of your input elements, userreplydisplay and userreplymessage are input textboxes. The setter and the getter are both .val(). You're using the getter correctly, but you're using the wrong setter.

$('#userreplydisplay').val('startreply ' + 'middlereply ' +
    $('#usernames').val() + ' endreply ' + $('#userreplymessage').val());

Change "text" to "val" and you'll be fine.

Upvotes: 1

Related Questions