<div id="icon">
<div id="search">
</div>
</div>
$('#search').click(function(ev) {
ev.stopPropagation();
});
$("#icon").click(function() {
$('#search').fadeToggle(350);
});
$(document).on('click', function(e) {
if (!$(e.target).closest("#icon").length) {
$('#search').hide();
}
e.stopPropagation();
});
<div id="search">
<div class="search__icon">
</div>
</div>
#search{
width: 150px;
height: 50px;
background-color: #ff0000;
}
#search.opened{
background-color: #0000ff;
}
#search.opened .search__icon{
background-color: #ffff00;
}
.search__icon{
width: 50px;
height: 50px;
background-color: #00ff00;
}
$("#search").on("click", function(){
$(this).toggleClass("opened");
});