title: Setting focus after an Ajax call
body: <p>I'm working on a user input page that should allow for speedy data entry. There is a barcode scanner attached to the computer that sends the code + chr(13) to the highlighted input field.</p>
  
  <p>What I want to do is scan the barcode, search the database and when it finds the barcode, enter the data and move on to the next line.</p>
  
  <p>The code works fine, and focus is being set to the new element. However, it looks like the Ajax-call is stealing the focus back to the calling element. How can I resolve this?</p>
  
  <p>This is the full JavaScript code:</p>
  
  <pre><code>$(document).ready(function() {
  
    function newTableRow(e) {
      var keyCode = e.keyCode;
      if (keyCode !== 9) return;
  
      if ($(e.delegateTarget).parent().is(':last-child')) {
        addTableRow();
      }
    }
  
    function addTableRow() {
      $('#tblData &gt; tbody tr:last').after(`&lt;tr&gt;
                              &lt;td&gt;&lt;input type=\"text\" size=\"20\" name=\"ean\" /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;input type=\"text\" size=\"20\" name=\"key\" /&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;input type=\"text\" size=\"40\" name=\"title\" /&gt;&lt;/td&gt;
                          &lt;/tr&gt;`);
  
      // Re-bind the events to the new tablerow
      $('#tblData &gt; tbody tr td:last').on('keydown', 'input', null, addTableRow);
      $('#tblData &gt; tbody tr td').on('keydown', 'input', null, searchDB);
      // Set focus to first element for keyboard input
      $('#tblData &gt; tbody tr:last td:first').focus();
    }
  
    function searchDB(e) {
      var keyCode = e.keyCode;
      if (keyCode !== 13) return;
  
      var value = $(e.target).val();
      var tablerow = $(e.target).closest('tr');
      var addrow = false;
  
      $.ajax({
          cache: false,
          type: "GET",
          url: "searcharticles.php",
          dataType: "xml",
          data: {
            search_param: value
          }
        })
        .done(function(xml) {
          var id = 0;
  
          $(xml).find('artikel').each(function() {
            id = $(this).find('id').text();
            if (id == -1) {
              tablerow.find('td input:eq(2)').val('Not found');
            } else {
              tablerow.find('td input:eq(1)').val($(this).find('key').text());
              tablerow.find('td input:eq(2)').val($(this).find('title').text());
              addrow = true;
            }
          });
        })
        .fail(function(xhr, ajaxOptions, thrownError) {
          alert(xhr.status);
          alert(thrownError);
        })
        .always(function() {
          if (addrow &amp;&amp; $(e.delegateTarget).parent().is(':last-child')) {
            addTableRow();
          }
        });
    }
  
    $('#tblData &gt; tbody tr td:last').on('keydown', 'input', null, newTableRow);
    $('#tblData &gt; tbody tr td').on('keydown', 'input', null, searchDB);
  
  });
  </code></pre>