Q: Not detecting keypress on input field Javascript I've tried everything and if anyone could explain why this JavaScript code is not causing the alert button to be shown when I press enter on the HTML input field, that would be greatly appreciated.


    document.getElementById("inp").addEventListener("keypress", clickedEnter, false);
    
    function clickedEnter(event){
        if(event.key == "Enter"){
            alert("true");
        }
    }
<input type = "text" class = "inp" id = "inp" placeholder="Add Item"> 



A: It works fine, I think you might have an issue if the DOM has not loaded before your javascript has run. In which case you can put your script at the end of the body or run the javascript after the DOM has loaded, for example:


window.addEventListener('DOMContentLoaded', (event) => {

  function clickedEnter(event){
      if(event.key == "Enter"){
          alert("true");
      }
  }
  
  document.getElementById("inp").addEventListener("keypress", clickedEnter, false);

});
<input type = "text" class = "inp" id = "inp" placeholder="Add Item">


