const icon = document.querySelector('.icon');
const search = document.querySelector('.search').value;
function searchbar() {
icon.style.display = "block";
return
}
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100vh;
width: 100%;
display: grid;
place-items: center;
font-family: Arial, Helvetica, sans-serif;
background-color: rgb(9, 5, 31);
}
.container {
position: relative;
}
.container .icon {
display: none;
position: absolute;
top: 50%;
right: 10px;
height: 20px;
transform: translateY(-50%);
}
.container .search {
width: 300px;
height: 40px;
border-radius: 9999px;
padding-left: 20px;
}
<div class="container">
<img class="icon" src="https://img.icons8.com/ios-filled/50/000000/search--v1.png" />
<input class="search" oninput="searchbar()" on type="text" placeholder="search">
</div>
To display the search icon next to the searched word, simply place both the icon and the search bar within the same parent element. Set the icon's position to 'absolute' and the parent's position to 'relative'.
To make the search icon appear while typing, initially set its display to 'none' and use JavaScript to show it when input length is greater than 1 through setting display to 'block'.
There are multiple ways to achieve this functionality.