DJR
DJR

Reputation: 474

how to duplicate the value of one form field in another in jquery

I have a form where a user can chose the applications he has access to? and when he the applications he wants access to, he is presented with more options. One of them is notification email address. if the user choses two applications, he gets two notification email address fields to fill. But both of them are stored in the same column in the database.

There are multiple fields like user authorisation level, user branch etc which are uniform accross all applications.

i am trying to replicate the value of one field in the other with jquery. i tried the below which obviously fails. Can anyone suggest how to acheive the replication?

$("#email").val().change(function() {       
    $("#email1").val($("#email").val());
});

Edit: I have drop down fields as well as text boxes.

Upvotes: 0

Views: 2072

Answers (2)

John Lee
John Lee

Reputation: 1387

It is an often asked question:

Copy another textbox value in real time Jquery

depending on when you need this action to execute, but if live

$(document).ready(function() {
    $('#proname').live('keyup',function() {
        var pronameval = $(this).val();
        $('#provarname').val(pronameval.replace(/ /g, '-').toLowerCase());
    });
});

http://jsfiddle.net/KBanC/

another one, basically same: JQUERY copy contents of a textbox to a field while typing

Upvotes: 0

scrappedcola
scrappedcola

Reputation: 10572

.val() returns a string which cannot have a change event. You need to use

$("#email").change(function() {       
    $("#email1").val($(this).val());
});

You will want to bind the change event using on or live depending on your version of jquery, if you haven't wrapped this piece of code in a ready block.:

$("#email").on("change",function() {       
    $("#email1").val($(this).val());
});

This fiddle shows setting a <select> tags value using .val() http://jsfiddle.net/8UG9x/

Upvotes: 2

Related Questions