2

How do I make the enter key press the search button? Thanks in advance.

function search(query) {
 if (query.slice(0, 7) == "http://") {
 // window.location.href = query
 window.open(query, '_blank');
 } else {
 // window.location.href = "https://www.google.com/search?q=" + query
 debugger;
 window.open("https://www.google.com/search?q=" + query, '_blank');
 }
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="box">
 <div class="container-1">
 <input type="search" id="search" placeholder="Search..." />
 <div class="icon" onclick="search(document.getElementById('search').value)"><i class="fa fa-search"></i></div>
 </div>
</div>

Linux Torvalds
4,1293 gold badges24 silver badges49 bronze badges
asked Jul 26, 2020 at 18:52
1
  • Why not just replace div with the actual submit button? Not having submit can have many problems with mobile devices, where you will spend a lot of time fixing them. When submit is pressed you can halt the request and process data as needed. No need to have special key triggers or other stuff. Assuming you are using input withing the scope of a form. Commented Jul 26, 2020 at 19:13

3 Answers 3

2

Here is an example:

// Get the input field
var input = document.getElementById("myInput");
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
 // Number 13 is the "Enter" key on the keyboard
 if (event.keyCode === 13) {
 // Cancel the default action, if needed
 event.preventDefault();
 // Trigger the button element with a click
 document.getElementById("myBtn").click();
 }
});

That should answer your question.

answered Jul 26, 2020 at 19:05
Sign up to request clarification or add additional context in comments.

1 Comment

Flawless, different issue than OP, but I commonly forget about the power of adding simple EventListeners. I used this method to hook the icon that is basically an end adornment on a BS5 input field. When using that button to call a function rather than submit a form, you'll need something like this. This should be accepted as it seems the most pure answer to OP. Nothing extra, just exactly what was asked for!
1

Actually it can be done simply by using a form and retrieve the input by name.

Notice we can even remove the button <input type="submit">, we can still use the ENTER key.

I left the simplest logic:

function search(query){
 query.preventDefault();
 let vquery = query.target.elements["search"].value;
 console.log(vquery)
 
}
something.addEventListener("submit", search, false)
<div class="box">
 <div class="container-1">
 <form id="something">
 <input name="search" placeholder="Search... type ENTER to submit or use the button -->">
 <input type="submit"> 
 </form>
 <div class="icon" ></div>
 </div>
</div>

answered Jul 26, 2020 at 19:51

Comments

1
const searchBtn = document.getElementById("button-search");
const searchInput = document.getElementById("search-field");
searchInput.addEventListener("keypress," function(event))
if (event.key == 'Enter')
 {
 searchBtn.click();
 }
});
answered May 8, 2023 at 6:59

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.