Unfortunately, it seems to be a bug with the .click()
command: https://stackoverflow.com/a/5081144. In summary (quoted from above link):
- If you mousedown on the padding move the mouse and mouseup on the padding, click event fires.
- If you mousedown on the padding move the mouse but now mouseup on the text, there is no click event.
Since you need to use the .click()
command, here may be a fix: http://jsfiddle.net/yg775/12/.
JQuery:
$('#button_box').mousedown(function() {
clicked = true;
});
$('#button_box').mouseup(function() {
if (clicked) {
paddingClicked = true;
$("#button_box").trigger( "click" );
}
});
$(document).mouseup(function() {
if (!paddingClicked) {
clicked = false;
}
});
$('#button_box').click(function() {
clicked = false;
if (paddingClicked) {
numClicks++;
$('#display').text(numClicks.toString()+' clicks');
paddingClicked = false;
}
});
I have a container div called button_box
that gets used with .mousedown()
and .mouseup()
. These set two flags, which then calls .trigger( "click" )
that simulates a click.
The $(document).mouseup(...)
is in place to catch if you click in the button and then drag the mouse outside before the .mouseup()
buttons is called to reset the flag. Without it, you can then click outside of the button and then drag the mouse back in and it would register the .mouseup()
. A bit hacky, but it works.