Копировать текст в буфер обмена js or jquery

217
04 мая 2018, 09:26

Есть блок в который через бекенд передается значение

<div   id="tech-inf" class="tech_info" data-help="Select to copy">
              {{ pw.account_info_config|tojson }}

как сделать на js или jquery что бы я смог по нажатию на блок скопировать тект который приходит мне?

$("#tech-inf").click(function () {
  CopyToClipboard('info_tech');
});
function copyToClipboard(elementId) {
  // Create a "hidden" input
  var aux = document.createElement("input");
  // Assign it the value of the specified element
  aux.setAttribute("value", document.getElementById(elementId).innerHTML);
  // Append it to the body
  document.body.appendChild(aux);
  // Highlight its content
  aux.select();
  // Copy the highlighted text
  document.execCommand("copy");
  // Remove it from the body
  document.body.removeChild(aux);
}

так не работает(

Answer 1

function copyToClipboard(elementId) { 
 
  // Create a "hidden" input 
  var aux = document.createElement("input"); 
 
  // Assign it the value of the specified element 
  aux.setAttribute("value", document.getElementById(elementId).innerHTML); 
 
  // Append it to the body 
  document.body.appendChild(aux); 
 
  // Highlight its content 
  aux.select(); 
 
  // Copy the highlighted text 
  document.execCommand("copy"); 
 
  // Remove it from the body 
  document.body.removeChild(aux); 
   
  console.log(document. 
 
} 
 
$("#tech-inf").click(function () { 
  copyToClipboard('tech-inf'); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div   id="tech-inf" class="tech_info" data-help="Select to copy"> 
{{ pw.account_info_config|tojson }} 
</div>

READ ALSO
Как л чше хранить данные из GridView

Как л чше хранить данные из GridView

Извиняюсь за прошлую тему, был не в курсеЕсть приложение семейный бюджет

280
Как переключатся между окнами JFrame?

Как переключатся между окнами JFrame?

С Swing начал работать недавно и вот возникла такая проблема

244
Jpa репозиторий и Sax

Jpa репозиторий и Sax

Решил парсить XML файлы с помощью SAX и заливать в бд, но столкнулся с небольшой проблемой: Есть классы: -public class MySaxHandler extends DefaultHandler

254
Snackbar и проблемы с его реализацией

Snackbar и проблемы с его реализацией

Я добавил эту библиотеку comandroid

296