Skip to content

Instantly share code, notes, and snippets.

@rqreyes
Last active January 4, 2018 04:04
Show Gist options
  • Save rqreyes/d7334f3e37b7285d9ec5486da18910d9 to your computer and use it in GitHub Desktop.
Save rqreyes/d7334f3e37b7285d9ec5486da18910d9 to your computer and use it in GitHub Desktop.
when clicking outside, hide searchbox
$('.search-trigger').click(function () {
$(this).toggleClass('active');
$("#searchbox").toggle();
$("#searchbox input").focus();
});
if (window.matchMedia("(min-width: 48em)").matches) {
var exclude_div = $("#searchbox, #searchbox *");
$(document).click(function (e) {
if (!(exclude_div.is(e.target)) && !($(e.target).hasClass('search-trigger'))) {
$('.search-trigger').removeClass('active');
$("#searchbox").hide();
}
});
}
$(window).resize(function () {
if ($(window).width() < 751) {
$("#searchbox").show();
$(document).click(function (e) {
$("#searchbox").show();
});
} else {
$("#searchbox").hide();
var exclude_div = $("#searchbox, #searchbox *");
$(document).click(function (e) {
if (!(exclude_div.is(e.target)) && !($(e.target).hasClass('search-trigger'))) {
$('.search-trigger').removeClass('active');
$("#searchbox").hide();
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment