I have a search bar that searches the input on google and it works fine. I was trying to add an on keypress enter functionality to run the search when I press enter but it's not working I added a console.log to debug if my function searchGoogle
was running and it passes I don't get how it's not searching on google on keypress enter though. When the search icon is pressed it works. Any help is appreciated. Thanks in advance.
function doSomething(event) {
console.log(event.keyCode);
if (event.keyCode == 13) {
searchGoogle();
}
}
function searchGoogle() {
var input = document.getElementById("googleSearchInput");
document.getElementById("googleSearchButton").href =
(("https://www.google.com/search?q=") + (document.getElementById("googleSearchInput").value));
console.log(input.value);
}
.form-group {
width: 50%;
position: absolute;
top: 3%;
left: 4%;
z-index: 1;
}
.form-control:focus {
border-color: #ff80ff;
box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.075) inset, 0px 0px 8px rgba(255, 100, 255, 0.5);
}
.form-group img {
width: 4%;
height: auto;
position: absolute;
top: 50%;
right: 1%;
margin-right: 1%;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<div class="form-group ">
<label class="poweredByGoogle">Powered By Google</label>
<input type="text" class="form-control" id="googleSearchInput" placeholder="Search The Web" onkeyup="doSomething(event)">
<a href="" id="googleSearchButton" onclick="searchGoogle()" target="_target"> <img src="https://img.icons8.com/material-outlined/128/000000/search--v1.png" /></a>
</div>