Doidgey Doidgey - 1 year ago 108
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")) {


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


Answer Source

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

$(document).click( function () {

To this

$(document).on('click touchstart', function () {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download