Ketan
Ketan

Reputation: 79

jQuery .select() is not working on mobile devices

I am working on one site where the requirement is to allow visitors to copy a code using single click. Something like we used to copy coupon codes on some sites using single click.

For this I've used jQuery as below:

$(".enter-coupon-input").on("click", function () {
   $(this).select();
}).trigger( "click" );

This is working on all browsers on desktop, it is also working on tablets including iOS and Android. But it is not working on any Mobile devices. When I try to select the text inside text field the whole text field get selected, not just text inside text field.

Here is the text field:

<input type="text" name="couponcode" id="couponcode" 
 class="enter-coupon-input enter_input_coupon" 
 value="<?php echo $text_here;?>" readonly />

Can anyone please tell me why this is not working, I've tried a lot by googling and searching code forums but yet not got any solution.

Upvotes: 2

Views: 1558

Answers (1)

kidwon
kidwon

Reputation: 4514

Try this, perhaps there's some default behavior in mobile browsers for this case

$(".enter-coupon-input").on("click", function (е) {
   e.preventDefault();
   $(this).select();
}).trigger( "click" );

Upvotes: 2

Related Questions