3

I have this function and i call it with onClick. On Google Chrome (windows) is working but on the Android Chrome is not. How can i make it to work ontouch, in Android Chrome?

Thank you!

function copyCupon(cupon, link) {
  var copyText = document.createElement("input");
  document.body.appendChild(copyText);
  copyText.setAttribute("id", "copyTextId");
  copyText.setAttribute('value', cupon);
  copyText.select();
  document.execCommand("copy");
  document.body.removeChild(copyText);
  alert("Cuponul " + "'" + copyText.value + "'" + " a fost copiat. Spor la cumparaturi! :)");
  window.open(link);
}
<button onclick="copyCupon('test to copy', 'http://www.google.ro')">Copy coupon</button>

1
  • 2
    "How can i make it to work ontouch". Listen ontouch event. Commented Feb 7, 2018 at 10:31

3 Answers 3

5

Try this:

  • Remove any hover rules or any CSS rules for the button and check if it works.
  • Add cursor pointer to the button style
  • Use jquery touchstart:

$('#whatever').on('touchstart click', function(){ /* your code */ });

Sign up to request clarification or add additional context in comments.

Comments

2

Adding cursor: pointer; to the css does the trick for me, but can't offer any explanation why.

function do_something() {
alert('It works now!')
}
.button {
  cursor: pointer;
}
<button class='button' onclick='do_something()'>Click</button>

.button {
  cursor: pointer;
}

Comments

1

Here's a fiddle using jQuery.

$('#btn').on('touchstart click', function() {
  var cupon = $('#btn').attr('val1');
  var link = $('#btn').attr('val2');
  var copyText = document.createElement("input");
  document.body.appendChild(copyText);
  copyText.setAttribute("id", "copyTextId");
  copyText.setAttribute('value', cupon);
  copyText.select();
  document.execCommand("copy");
  document.body.removeChild(copyText);
  alert("Cuponul " + "'" + copyText.value + "'" + " a fost copiat. Spor la cumparaturi! :)");
  window.open(link);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='btn' val1='test to copy' val2='http://www.google.ro'>Copy coupon</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.