Doidgey Doidgey - 4 months ago 14
jQuery Question

document .click function for touch device

I've got a sub-nav that works using jquery - A user clicks on the top level list item, for instance 'services' which triggers the dropdown. The dropdown toggles via clicking the 'service' link. I've made it so a user can click anywhere on the screen to toggle the dropdown to a closed state. But as the site is responsive i want the user to be able to click (touch) anywhere on the screen to close the dropdown but my problem is that it's not working on the touch devices.

My code ive setup for the document click is:

$(document).click(function(event) {

if ( $(".children").is(":visible")) {
$("ul.children").slideUp('slow');
}

});


I'm assuming document.click might not work on touch devices, and if not, what work-around is there to achieve the same effect?

Thanks

Answer

To trigger the function with click or touch, you could change this:

$(document).click( function () {

To this

$(document).on('click touchstart', function () {
Comments