As I understand it, the problem is that the icon has two tasks
- expand search input first
- after that act as a submit button. Perhaps even check if the input to the search is relevant. If yes, execute a request, for example, a message via ajax, otherwise do not do anything or show a message that the user must fill out something.
Work with CSS :focus
One problem is that every time you click on the icon, the search input loses focus because your icon is not the content of this input. This way :focus , which you add using CSS, will be removed. One solution would be to work with pseudo-elements and simply place the icon in the contents :after or :before , but since you cannot use them in the inputs, you need to make a workaround to achieve what you want. Therefore, in your case, I believe that the only way is to simulate focus using JavaScript.
This can be done using the following code
HTML
<div id="search"> <button id="search-button"> <i class="ion-ios-search"></i> </button> <input type="search" id="search-input" /> </div>
CSS
* { box-sizing: border-box; } html, body { height: 100%; } body { margin: 0; background: #eee; } input { display: none; } #search { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); height: 40px; font-size: 0; } #search-button, #search-input { display: inline-block; background: white; border: none; outline: none; } #search-button { position: relative; width: 40px; height: 100%; border: none; cursor: pointer; } #search-button i { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 30px; margin: 0; } #search-input { height: 100%; width: 0; font-size: 20px; vertical-align: top; transition: 0.3s; padding: 0; } .clicked + #search-input { width: 200px; }
JQuery
var $searchButton = $("#search-button"); var $searchInput = $("#search-input"); $searchButton.click(function() { $searchInput.focus(); if ($searchInput.val() !== "") { alert("post"); } else if (!$(this).hasClass("clicked") && $searchInput.val() === "") { $(this).addClass("clicked"); } else if ($(this).hasClass("clicked") && $searchInput.val() === "") { alert("fill"); } }); $(document).click(function(e){ if(!$("#search").has(e.target).length && $searchInput.val() === ""){ $searchButton.removeClass("clicked"); } });
var $searchButton = $("#search-button"); var $searchInput = $("#search-input"); $searchButton.click(function() { $searchInput.focus(); if ($searchInput.val() !== "") { alert("post"); } else if (!$(this).hasClass("clicked") && $searchInput.val() === "") { $(this).addClass("clicked"); } else if ($(this).hasClass("clicked") && $searchInput.val() === "") { alert("fill something in"); } }); $(document).click(function(e) { if (!$("#search").has(e.target).length && $searchInput.val() === "") { $searchButton.removeClass("clicked"); } });
* { box-sizing: border-box; } html, body { height: 100%; } body { margin: 0; background: #eee; } input { display: none; } #search { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); height: 40px; font-size: 0; } #search-button, #search-input { display: inline-block; background: white; border: none; outline: none; } #search-button { position: relative; width: 40px; height: 100%; border: none; cursor: pointer; } #search-button i { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 30px; margin: 0; } #search-input { height: 100%; width: 0; font-size: 20px; vertical-align: top; transition: 0.3s; padding: 0; } .clicked + #search-input { width: 200px; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="search"> <button id="search-button"> <i class="ion-ios-search"></i> </button> <input type="search" id="search-input" /> </div>
Svenl source share