Upon the click of a link (that doesn't have an href attribute), I'd like the text of the link to be selected (on desktop and mobile), and, on mobile devices, for the copy menu to be presented. For this purpose, I've tried to do the following (code originally from here, and here's a jsfiddle):
function selectText(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
}
}
$('.mylink').click(function() {
selectText($(this).attr('id'));
});
On desktop systems, upon clicking, the link in the text is selected, as expected - that is, on desktop, the current code is achieving the desired behavior.
On Android, the link in the text is selected upon clicking, but the copy menu does not come up, as one would expect when text is highlighted.
On Android/mobile devices in general, upon touching a link, I'd like the text of the link to be selected, and the copy menu to be presented. What's a good way to accomplish this?