Skip to content

Instantly share code, notes, and snippets.

@bluespore
Last active December 14, 2015 10:09
Show Gist options
  • Save bluespore/5069465 to your computer and use it in GitHub Desktop.
Save bluespore/5069465 to your computer and use it in GitHub Desktop.
Search form that grows / reveals on click of an element.
$('#toggle_search').on('click', function()
{
var form = $(this).closest('form');
//If search not already open, open it
if(!form.hasClass('open')){
form.addClass('open');
//Allow animation of form, then trigger focus on field
setTimeout(function(){
form.find('input').trigger('focus');
}, 400); //timeout is time of animation
}
//Form is open, so close it
else{
form.removeClass('open');
setTimeout(function(){
form.find('input').trigger('blur');
}, 400);
}
});
//Prevent closing on input click
$('#form_search').on('click', function(e){
e.stopPropagation();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment