Kukula Mula
Kukula Mula

Reputation: 1869

javascript AddEventListener `click` works on desktop not on tablet

the following code works perfect on desktop but not on tablet

card.querySelector("#img").addEventListener('click', this.zoomIn.bind(this));

I'm testing it on Samsung SMT510 although I've enabled the dev tools, don't know how (or if possible) to have a dev-tools console debug and breakpoints

Any ideas for the reasons click doesn't take?

Upvotes: 0

Views: 240

Answers (1)

Kenneth Lew
Kenneth Lew

Reputation: 237

Touch does not count as a click in javascript. Instead, you can do something like this to emulate touch as a click:

function onTap(elem, cb) {
  function onTouchStart(evt) {
    elem.addEventListener("ontouchend", onTouchEnd)
  }

  function onTouchEnd(evt) {
    elem.removeEventListener("ontouchstart", onTouchStart)
    elem.removeEventListener("ontouchend", onTouchEnd)
    cb(evt)
  }
}

// usage: 
const elem = document.querySelector('#card')
onTap(elem, this.zoomIn.bind(this)))
<div id='card'></div>

Upvotes: 0

Related Questions