I have this form:
<form>
<label for="locationsearch">Location:</label>
<input type="search" id="locationsearch" name="locationsearch" />
</form>
I want to add an eventListener when I hit enter on the input(i.e. #locationsearch). I tried doing this:
const locationSearch = document.getElementById("locationsearch");
locationSearch.addEventListener("search", () => {
console.log("search entered");
});
and this:
const locationSearch = document.getElementById("locationsearch");
locationSearch.onsubmit = function () {
console.log("search entered");
};
Both are not console-logging.
What is the correct/better way to perform this action?
The onsubmit event would happen on the form itself, not the input. So you could use an id on the form instead to target it directly.
const locationSearch = document.getElementById("locationsearch");
locationSearch.onsubmit = function () {
console.log("search entered");
};
<form id="locationsearch">
<label for="locationsearch">Location:</label>
<input type="search" name="locationsearch" />
</form>
You could handle it keydown event handler of input element. And check the key code if Enter key pressed.
const locationSearch = document.getElementById("locationsearch");
locationSearch.addEventListener("keydown", (e) => {
if (e.code === 'Enter') {
// Do Something ? Search
}
});
You can use keypress event for this.
const locationSearch = document.getElementById("locationsearch");
locationSearch.addEventListener("keypress", () => {
if (event.key === "Enter") {
event.preventDefault();
let inputVal = document.getElementById("locationsearch").value;
console.log("search entered "+inputVal);
document.getElementById("locationsearch").value = "";
}
});
<form>
<label for="locationsearch">Location:</label>
<input type="search" id="locationsearch" name="locationsearch" />
</form>
You may also like…
- Why fluentd loss most of it's log while using http output plugin (in similar timestamp)
- Java Mouse Listener – Can event type be detected inside of an event?
- flutter build ipa works locally, but not when run on github actions on the same machine
- CSS Imported by a Dependency
- Module '"rxjs"' has no exported member 'firstValueFrom'
- what use spring multiple handler websocket session service
- Failing to increment pair count correctly add_pairs Tideman CS50
- How can I get the average cost from a specific column from a table in R
- The theme system is not working for me and I'm not sure why
- Sorting multiple rows of un-sorted data based on text string – GSheets
- Tkinter RuntimeError: main thread is not in main loop when trying to insert item into listbox from different thread
- SwiftUI: Generating a continuous alphabetical list (from a-z then from aa, bb, cc, …, aaa, bbb, ccc)
- AWS Eventbridge rule trigger once all in-flight sqs messages are processed
- Avoid calling a method in a loop
- How to add a single value in datagridview with c#?
- MenuItem with onClick={popupState.close} in Material-ui V 5.0 how to execute a Link to open the component?
amazon-web-services android angular api arrays c# css dart dataframe django docker excel express firebase flutter html ios java javascript jquery json kotlin laravel linux list mongodb mysql node.js pandas php postgresql python python-3.x r react-native reactjs regex spring spring-boot sql sql-server string swift typescript vue.js