document.getElementById("yourElementId").addEventListener("keypress", function (event) { if (event.key === "Enter") { // the code you want to run } })