-1

I have a demo link that I want to add click event for window. It works fine for desktop but not for mobile.

Basically I run tests on lastest Chrome and Safari,

  $('#button1').click(function(){
     $('#text').append('<div> jquery click</div>')
     // this would get executed on both desktop and mobile
  })
    window.addEventListener('click', function() {
     $('#text').append('<div> native click on window</div>')
     // this only gets executed on desktop
  })

So my question is, why the click event doesn't fire when on mobile?

ydydyd
  • 71
  • 2
  • 9
  • 1
    These are all very generic statements ... Please include the **code** for which the `click` events are not happening .. Examples of what works in what browser etc etc .. – Zak Jan 24 '18 at 16:50
  • Can you click a screen? :) – Zoe Edwards Jan 24 '18 at 16:50
  • Possible duplicate of [Safari on iOS 9 does not trigger click event on hidden input file](https://stackoverflow.com/questions/32708496/safari-on-ios-9-does-not-trigger-click-event-on-hidden-input-file) – Zoe Edwards Jan 24 '18 at 16:50
  • @ThomasEdwards Can you remove the "possible answered question" please? It's not a similar question, – ydydyd Jan 24 '18 at 17:40

2 Answers2

0

MDN shows that for Safari, if an element is not considered 'clickable', no click event would fire. I would assume that window is not considered 'clickable' so no 'click' event would fire by window (except the ones propagated by other clickable elements).

There is also a Safari link to explains this.

Though I didn't find anything on Chrome, I would assume since it adopts the same idea.

ydydyd
  • 71
  • 2
  • 9
0

Apparently Safari doesn't consider the window a clickable event.

As ydydyd pointed out, the Safari docs goes into a more detailed explanation.

To fix it, simply replace window.addEventListener() with document.addEventListener()

Community
  • 1
  • 1
Noah
  • 174
  • 1
  • 2
  • 10