Skip to content

Instantly share code, notes, and snippets.

@sturobson
Created February 5, 2014 13:50
Show Gist options
  • Select an option

  • Save sturobson/8823990 to your computer and use it in GitHub Desktop.

Select an option

Save sturobson/8823990 to your computer and use it in GitHub Desktop.
What's going on with my JS
So I've got this code
var $tooltips = $('.tooltip__link');
$tooltips.on('click touchstart', function(){
var tooltip = $(this);
tooltip.toggleClass('js-opened');
return false;
});
$('body').on('click touchstart', function(){
$tooltips.removeClass('js-opened');
});
I'm using touchstart here as iOS doesn't seem to wanna work without it.
unfortunately
$('body').on('click touchstart', function(){
$tooltips.removeClass('js-opened');
});
when tapping it invokes the form elements underneath the tooltip that's showing
doing this
$('body').on('click touchstart', function(){
$tooltips.removeClass('js-opened');
return false;
});
fixes this but it seems to hijack the page for any other click events.
Any ideas ?
@steveworkman

Copy link
Copy Markdown

You should also turn the main body event off when the tooltip isn't open to prevent the click-jacking:

var removeEvent = function (e) {
$tooltips.removeClass('js-opened');
$('body').off('click', removeEvent);
}

$tooltips.on('click touchstart', function(e){
   e.preventDefault();
    var tooltip = $(this);
    tooltip.toggleClass('js-opened');
    $('body').on('click', removeEvent);
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment