I have a nav header with a data attribute
<nav class="nav-header" data-drawer-display="show" data-drawer-num="0">
that sets the display of the navigation to either show or hide on click of a nav link, with data-drawer-display="". I need to be able to toggle the navigation away by clicking on the body of the page with
$("body").on("click", function(){});
that should toggle the value from show to hide. I have tried this:
$('body').on('click touchstart',function(e){
$( ".nav-header" ).attr( "data-drawer-display", function( i, val ) {
return val === 'show'?'hide':'show';
});
but the logic seems to be off.
SOLUTION
A modified version of @empiric below:
$('body').on('touchstart', function(e){
//keeps nav from closing when clicking in subnav and links
if ($(e.target).is('.opener, .nav-sub, a')) {
console.log(target);
return false;
}
//if subnav is open, close on body click.
$( ".nav-header" ).attr( "data-drawer-display", function( i, val ) {
return val === 'show' ? 'hide' : '';
});
});