2

I am developing a cordova app. Because of the 300ms delay with the click event I don't want to use that. How do I change the event a <input type='checkbox'> element listens to for toggeling it's checkmark from click to e.g. touchstart?

The closest I got was making the checkbox disabled and adding this code:

$(':checkbox').each(function(){
        $(this).bind('touchstart', function(){
            if(this.checked) {
                this.checked = true;
            } else {
                this.checked = false;
            }
        })
    });

Unfortunately touchstart does not trigger.

David Schumann
  • 13,380
  • 9
  • 75
  • 96
  • Have you thought about using fastclick (https://github.com/ftlabs/fastclick)? It eliminates the 300ms click delay. – David Jul 05 '17 at 21:25

4 Answers4

1

With that code checkbox event will called twice. First touchstart event will called and then after 300ms delay click event will called real event checkbox. Let's try this code

$(':checkbox').each(function(){
    $(this).bind('touchstart', function(){
        if(this.checked) {
            this.checked = true;
        } else {
            this.checked = false;
        }
    }).bind('click', function(e) {
        e.preventDefault();
    });
});
Yusril Herlian Syah
  • 497
  • 1
  • 6
  • 12
  • The code does not work like that, but e.preventDefault() was a key function I was missing. So you earned that bounty :) For a working version, see my answer – David Schumann Jul 07 '17 at 14:04
0

you can use this code

   document.querySelector("#idofelement").addEventListener("touchend", function() {


    }
  • Wouldn't this still have the old EventListener listening for click events on the checkbox, making it react twice? – David Schumann Jun 25 '17 at 18:01
  • Well it won't work like this, as the callback function you provide is empty. But it won't unsubscribe the normal `click` event either. – David Schumann Jun 25 '17 at 18:28
0

for one, I need to call preventDefault() on the caught event.

The other problem was, that checked is not an actual jquery property of checkboxes, and I need to work with prop()

So what ended up working was:

$('.divClassContainingCheckbox').each(function(){
    $(this).bind('touchstart', function(e){
        e.preventDefault();  // prevents click from triggering check
        checkbox = $(this).find(':checkbox');
        if(checkbox.is(":checked")) {
            checkbox.prop('checked', false);
        } else {
            checkbox.prop('checked', true);
        }
    })
});

HTML:

<div class='divClassContainingCheckbox'>
    <input type='checkbox' />
</div>
David Schumann
  • 13,380
  • 9
  • 75
  • 96
0

This function works for me, I get the attr for checkbox label, and then I checked the input type checbox programaticaly

function checkedCheckbox() {  
  var id = $(this).attr('for');  
  var element = $('#'+id);
  var isChecked = element.prop("checked");

  if(isChecked) {
    $(element).attr("checked", false);
  } else {
    $(element).prop('checked', true);
  }
}
GMB
  • 216,147
  • 25
  • 84
  • 135