I am trying to create the same interactive searchbar as apple.com (and I know that there are many other posts that ask similar questions), and I am trying to use JS to toggle the searchbar.
My approach was with JQuery:
// inserts a new search_bar
$(".glyphicon.glyphicon-search").on("click", function(){
var search_bar = document.createElement("input");
search_bar.setAttribute("type", "text");
search_bar.setAttribute("class", "form-control search left rounded");
search_bar.setAttribute("id", "search-bar");
search_bar.setAttribute("placeholder", "search");
search_bar.setAttribute("ng-model", "search.name");
$(this).parent().replaceWith(search_bar);
});
HTML:
<span class="glyphicon glyphicon-search" style="color:white;"></span>
This function replaces the search glyphicon with the search (input tag) bar upon click. But I need a good way to snap the search bar back the the search glyphicon upon clicking else where.
Any Ideas?
Fiddle: http://jsfiddle.net/w9pwh7fr/1/
Update:
Using $('html').on("click", function() {}) seems to work for the off click and event.stopPropagation to prevent clicking on glyphicon to trigger both events.
Slight problem now with the search only toggling once. Upon first execution.
Updated Fiddle: http://jsfiddle.net/w9pwh7fr/3/