Search code examples
javascriptjquerykeypress

jQuery: Display value of a field in another field at .keypress() doesn't show


I have a field 1 and I want to show the value of this field 1 in another field 2.

When I insert the first letter in field 1, for example "G" nothing happes in field 2. But when I insert the second letter for example "Go", in field 2 only the letter "G" is shown. I tried .keypress() and .keydown(), but without success. Any ides or help?? Here is my code:

$("#field1").keypress(function() {
    var val = $("#field1").val().toLowerCase();
    $("#field2").val(val);
    });

Solution

  • use onkeyup

    $("#field1").keyup(function() {
        var val = $("#field1").val().toLowerCase();
        $("#field2").val(val);
        });
    

    to get the required result

    check fiddle here

    edit : FYI

    Its always prefer not to use keypress event as this event isn't covered by any official specification its behavior differ across browsers, browser versions, and platforms.