I have a code that work but I don't know how to factorize it. It seems I could be able to do it as they're basically the same code first for desktyop then for touch devices:
//desktop
$(document).mouseup(function (e)
{
var container = $(".messenger");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.empty();
container.off( 'click', clickDocument );
}
});
// Touch devices like IPAD and IPHONE we can use following code
$(document).on('touchstart', function (e) {
var container = $(".messenger");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.empty();
container.off( 'click', clickDocument );
}
});