Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery on click not working on iPhone (touch devices)

Can anyone please explain to me why this is working in the browser but not on mobile devices like Apple's iPhone. On iPhone, I never get the hello from the alert. Why?

<div class="close">
  Click here
</div>

JS:

$(document).on('click', '.close', function() {
  alert('hello');
});

Example here: https://jsfiddle.net/27ezjrqr/5/

like image 784
caramba Avatar asked Mar 11 '16 17:03

caramba


2 Answers

By default, divs are not a "clickable" elements. But adding cursor: pointer; makes iOS treat it as clickable.

So all you need to do is add

.close {
    cursor: pointer;
}

to your CSS and then it will work.

Proof here: https://jsfiddle.net/27ezjrqr/6/

$(document).on('click', '.close', function() {
  alert('hello');
});
.close {
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="close">
  Click here
</div>
like image 177
Gavin Avatar answered Nov 14 '22 20:11

Gavin


I used the touchstart event with the click , it did the job for me

 $(document).on("click touchstart tap", "#button_X", function (){
//your code here      
 });
like image 30
Lord Bolton Avatar answered Nov 14 '22 19:11

Lord Bolton