javascriptjqueryjquery-focusout

Why doesn't this jQuery focusout and change function work?


This must be simple, but I can't see what is preventing this combonation .focusout and .change function from working.

I want the first input to copy to the second when the focus leaves the first input. No errors in the console. Fiddle: https://jsfiddle.net/fh8t6mm0/

The reason I want to use focusout is that the .change and .val function doesn't work reliably for some reason.

HTML:

<div id="focusoutdiv">

  <input class="fieldone" type="text">

  <input class="fieldtwo" type="text">

</div>

JS:

$('#focusoutdiv').focusout(function() {
  $('.fieldone').change(function() {
    $('.fieldtwo').val($(this).val());
  });
});

Solution

  • You can also use blur event to copy text from one textbox to other. Below code mightbe helpful for you

    $('.fieldone').blur(function() {
        $('.fieldtwo').val($(this).val());
    });