-1

I have a function onfocusout() that runs another function selectEntite() inside an input like the following code :

<input onfocusout="selectEntite();" type="text" placeholder="Entité" id="entityName" class="inputSelect" name="entityName" style="width: 60%" />

when the user leave the input, the function run. I want to change this behaviour by making it run when the user tap the keyboard enter.

2 Answers2

0

Try this:

<input onkeydown="selectEntite(this);" type="text" placeholder="Entité" id="entityName" class="inputSelect" name="entityName" style="width: 60%" />
function selectEntite(ele) {
    if(event.key === 'Enter') {
        alert(ele.value);        
    }
}
Vimal
  • 73
  • 1
  • 12
0

you can add an addEventListener() to the input field that listens for key presses. Then check if enter is pressed, then you can execute some code. –

let el = document.getElementById("entityName")
el.addEventListener("keydown",(event) => {
    if (event.key === "Enter") {
        // Enter key was hit
    }
})
Kokodoko
  • 26,167
  • 33
  • 120
  • 197