Js has a built-in function that allows you to retrieve data from an input when a user enters something into it: input.addEventListener("input", myFunction()).
But it does not suit me, because it is executed every time the user enters at least one character. Instead, I need an approach in which the function would be called only when the user typed something and then pressed enter or clicked on a page in different place.
CodePudding user response:
You can use the focusout event which fires when an element is about to lose focus and for the enter key you could check the event.key within the keypress event which is fired when a key that produces a character value is pressed down. like so:
input.addEventListener("keypress", function (e) {
if (e.key === 'Enter') {
// code for enter
}
});
focusout - https://developer.mozilla.org/en-US/docs/Web/API/Element/focusout_event
keypress - https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event
CodePudding user response:
You can use the following methods for this:
use
onblurfor the input elements. This will trigger once the user clicks anywhere outside of that input box.use
onsubmitfor the form. This will trigger when the user presses enter or submits the form in anyway.
