I need the text to be copied by clicking, but not highlighted. I can not understand what method is responsible for the selection. Googled that selectNodeContents() selects text, but this does not work. Maybe someone can tell?
$(document).ready(function () { function selectText(elementId) { var doc = document, text = doc.getElementById(elementId), range, selection; if (doc.body.createTextRange) { range = document.body.createTextRange(); range.moveToElementText(text); range.select(); } else if (window.getSelection) { selection = window.getSelection(); range = document.createRange(); range.selectNodeContents(text); selection.removeAllRanges(); selection.addRange(range); } } $("#linkCopy").click(function() { selectText(this.id); document.execCommand("copy"); $('.link__url-icon').css("display", "block"); }); }); .link__url { display: flex; } .link__url-icon { display: none; margin-left: 15px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="link__url" id="linkCopy">https://www.google.com.ua/?hl=ru<span class="link__url-icon"><img src="./images/copy.svg"></span></span>