JavaScript Enter key function sometimes works and sometimes doesn't

I have a search box which should work with pressing the button and also pressing the Enter key. They both have the same code. The button works perfectly but when it comes to the Enter key, most of the time it doesn't work and sometimes it suddenly works.

Here's my code:

$(document).ready(function() {

  function myFunction() {
    $("#search-criteria").keydown(function(event) {
      if (event.keyCode === 13) {
        var txt = $('#search-criteria').val();
        $('.items:contains("' + txt + '")').addClass('searched');
      }
    });
  }

  $('#search').click(function() {
    var txt = $('#search-criteria').val();
    $('.items:contains("' + txt + '")').addClass('searched');
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" id="search-criteria" onkeydown="myFunction()" name="search" placeholder="Search..">
  <input type="button" id="search" value="search" />
</form>
Answer:1
  1. Remove the inline-event onkeydown you don't need it.

  2. You need to use preventDefault inside your myFunction function.

  3. Remove the classes inside your functions to see clearly the new search result.

    $(document).ready(function() {
      $("#search-criteria").keypress(function(event) {
        if (event.which === 13) {
          myFunction();
        }
      });
    
      //code for the button
      $('#search').click(myFunction);
    });
    
    var myFunction = function() {
      event.preventDefault();
    
      var txt = $('#search-criteria').val();
    
      $('.items').removeClass('searched');
      $('.items:contains("' + txt + '")').addClass('searched');
    }
    .searched {
      background-color: green;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <form>
      <input type="text" id="search-criteria" name="search" placeholder="Search..">
      <input type="button" id="search" value="search" />
    </form>
    
    
    <ul>
      <li class="items">Coffee</li>
      <li class="items">Tea</li>
      <li class="items">Milk</li>
      <li class="items">Coffee</li>
      <li class="items">Tea</li>
      <li class="items">Milk</li>
      <li class="items">Coffee</li>
      <li class="items">Tea</li>
      <li class="items">Milk</li>
    </ul>
    Answer:2

    I'm trying to do something I don't remember/find if it is possible in javascript. I have an object constructor "Point" and I want to know if it possible to, for example, sum two different objects ...

    I'm trying to do something I don't remember/find if it is possible in javascript. I have an object constructor "Point" and I want to know if it possible to, for example, sum two different objects ...

    1. properties object javascript
    2. object properties javascript loop
    3. object properties javascript class
    4. merge properties objects javascript
    5. add properties objects javascript
    6. properties inside objects javascript
    7. get properties object javascript
    8. iterate properties object javascript
    9. count properties object javascript
    10. delete properties object javascript
    11. list properties object javascript
    12. remove properties object javascript
    13. show properties object javascript
    14. filter properties object javascript
    15. foreach properties object javascript
    16. compare properties object javascript
    17. check properties object javascript
    18. copy properties object javascript
    19. update properties object javascript
    20. print properties object javascript

    I'm trying to start a git repo when scaffolding an application with a yeoman custom generator, this is what I got at the install step: install: function () { this.installDependencies(); ...

    I'm trying to start a git repo when scaffolding an application with a yeoman custom generator, this is what I got at the install step: install: function () { this.installDependencies(); ...

    1. handling error events

    I created an array buffer by var uni = new Uint8Array([255, 216, 255, 0, 0, 0, 0, 0]) But when I try to get back the bytes using map uni.map(byte => byte.toString(16)) it returns Uint8Array(8) ...

    I created an array buffer by var uni = new Uint8Array([255, 216, 255, 0, 0, 0, 0, 0]) But when I try to get back the bytes using map uni.map(byte => byte.toString(16)) it returns Uint8Array(8) ...

    I have a checkbox column inside Ext.grid.EditorGridPanel configured like this: { id: 'event-type-grid-enabled-checkbox', header: "Enabled", dataIndex: "enabled", sortable: true, ...

    I have a checkbox column inside Ext.grid.EditorGridPanel configured like this: { id: 'event-type-grid-enabled-checkbox', header: "Enabled", dataIndex: "enabled", sortable: true, ...

    1. checkbox inside grid
    2. checkbox inside gridview
    3. checkbox inside kendo grid
    4. checkbox inside kendo grid mvc
    5. checkbox inside ag-grid
    6. telerik checkbox inside grid