Trigger an event when the Enter key is pressed?

Hi!

So I’ve been struggling with this for a little while now.
I finished the Jamming project and I’ve been wanting to add the function so that when you press Enter on your keyboard it triggers the search.
Right now it is set in the way that you have to click with your mouse on the Search button, for the search to be triggered.

I was able to find some information online but I am not too sure how to incorporate it to my code?

If I understood correctly I need to add the following to my code:

const button = document.getElementById(“myInput”);

button.addEventListener(“keyup”, function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById(“myBtn”).click();
}
});

Any help would be greatly appreciated!

If you mean that before this, they filled out a form, then, forms already behave this way

<!DOCTYPE html>
<html>
  <head>
    <script>
      const dosomething = () => alert("hi")
    </script>
  </head>
  <body>
    <form onsubmit="dosomething()">
      <input type="text">
    </form>
  </body>
</html>

Thanks for your answer ionatan, however, it still doesn’t make much sense to me, am I missing something?

Idk what understanding/view you have on things so I couldn’t begin to guess if you’re missing something.
Does the code I showed behave the way you’re looking for?

It has a field where you can enter text, and pressing enter submits.
Which to me seems to match this:

Which would mean that you wouldn’t need to implement it at all, you could use a form.