1

I use this code to disable and enable touch:

jQuery(document).ready(function(){

 jQuery("body").on("touchmove", false);

 jQuery('button').click(function(){
     jQuery("body").on("touchmove", true);
 });

});

function work fine for disabling touch but after click on button touch cannot be enable.

whats wrong on code?

Morteza
  • 2,143
  • 7
  • 37
  • 61

2 Answers2

11

What is wrong is simply that you don't pass the argument required by on. You should pass a function (the event handler) and not just a boolean.

The simplest way to unbind then rebind is to have a boolean somewhere and test it in your handler. For example :

myapp = {active: true}; // change this boolean
jQuery("body").on("touchmove", function(e) {
    if (myapp.active) { // change myapp.active
        // do things
    }
});

If you want to unbind definitively, instead of passing false to on, use off. Passing false is a non documented trick that might break on future versions.

The off documentation also contains an example of another way to bind/unbind a function on click :

function aClick() {
  $("div").show().fadeOut("slow");
}
$("#bind").click(function () {
  $("body").on("click", "#theone", aClick)
    .find("#theone").text("Can Click!");
});
$("#unbind").click(function () {
  $("body").off("click", "#theone", aClick)
    .find("#theone").text("Does nothing...");
});
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
6

use bind() and unbind()

try this

 jQuery('body').bind('touchmove', function(e){e.preventDefault()});

 jQuery('button').click(function(){
     jQuery('body').unbind('touchmove');
  });
bipen
  • 36,319
  • 9
  • 49
  • 62
  • 2
    This will not make any difference, since jQuery internally uses `on()` to `bind()` events. Ref: https://github.com/jquery/jquery/blob/1.7/src/event.js#L965 – Luuuud Oct 13 '13 at 01:49