Are you sure that your method of detecting whether or not an element is toggled is correct? It looks like both #onclick
and #onclickJQ
share a common variable to detect which state they should be in: bt = 0;
. As a result if you try to press one after the other, the second click event will have no visible changes. To solve this, use attributes to store data about the toggle state...
JS:
function blub(selector) {
if ($(selector).attr("attr-bt") == "0") {
$(selector).attr("attr-bt", "1");
$(selector).css('background-color', '#aa0000');
}
else {
$(selector).attr("attr-bt", "0");
$(selector).css('background-color', '#333333');
}
}
$(document).ready(function() {
$('#onclickJQ').attr("attr-bt", "0");
$('#onclick').attr("attr-bt", "0");
});
Answering your question, to remove the delay, simply use jQueryMobile and the tap event that is provided with the library:
$(document).ready(function() {
$('#onclickJQ').live("tap", function() {
blub('#onclickJQ');
});
});
However, the first demo button [with the inline onclick event] must be modified to use the tap event specifically:
$(document).ready(function() {
$('#onclick').live("tap", function() {
blub('#onclick');
});
});
DEMO: http://fiddle.jshell.net/jnY2n/4/show/
Tested on an iPhone 4S; the first div "lags" when it is tapped repeatedly, while the second runs smoothly.