Using jQuery to auto-populate a form field from another form field

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2010-05-30T17:24:49Z Indexed on 2010/05/30 17:32 UTC
Read the original article Hit count: 225

Filed under:
|
|
|

Has anyone used jquery to take data from one form field and put it into another? I'm trying to create a form that when one text input is filled out a second is auto-populated with the first letter of the word that is in the first text input. I'm thinking I can limit the second text input to one character to help get the desired result, but I'm not having luck getting jquery to get the second text input and auto-populate once the first is entered. Here is the code I'm using:

<script type="text/javascript"> 
    jQuery(document).ready(function($) {
    $('#textBox1').keyup(function(){
    if($.trim($('#textBox2').val()) == '')
       $('#textBox2').val($(this).val().substring(0, 1);
});
});
</script>

Also, do I need to have any in the text input fields of the form other than matching "textBox1" id/names? Any suggestions?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery