'document.getSelection() not working on iOS chrome/safati
The problem that i'm having is that on iOS it seems like i cannot get the selection from the textarea. The same code on PC works well, and i can see the same text in the two alerts (range and sel, see code): on iOS unfortunately I see what i expect iun the 'range' alert but i see nothing in the 'sel' range (just the actual "sel :").
I tried almost every type of check possible and read all the existing documentations (i think at lest) before writing here so this is kinda my last resort
also the code is crappy, im sorry, just need this to work on mobile
// text area method
let textArea = document.createElement("textarea");
document.body.appendChild(textArea);
$(textArea).html(textToCopy);
// make the textarea out of viewport
textArea.style.position = "absolute";
textArea.style.opacity = 0; //tried without this, didnt work
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
//tried with this, didnt work
textArea.contentEditable = true;
textArea.readOnly = false;
$(textArea).css("user-select", "text");
$(textArea).css("-webkit-user-select", "text");
/*textArea.focus();*/
var selection = document.getSelection();
var range = document.createRange();
range.selectNodeContents(textArea);
alert("range: " + range)
selection.removeAllRanges();
selection.addRange(range);
alert("sel: " + selection.toString())
//tried with this, didnt work
/*textArea.setSelectionRange(0, 999999);*/
document.execCommand('copy')
textArea.remove();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
