Created
February 5, 2014 13:50
-
-
Save sturobson/8823990 to your computer and use it in GitHub Desktop.
What's going on with my JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ? | |
Try using preventDefault
rather than return false
like so:
$tooltips.on("click touchstart", function (e) {
e.preventDefault();
// Rest of your code (excluding return false)
});
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
do this
var$tooltips = $ ('.tooltip__link');
$tooltips.on('click', function(){
var tooltip = $(this);
tooltip.toggleClass('js-opened');
return false;
});
$('body').on('click', function(){
$tooltips.removeClass('js-opened');
});
and put
pointer: cursor; on the body