Я хочу скопировать значение ввода и textarea в буфер обмена - PullRequest
1 голос
/ 04 июня 2019

У меня есть вопрос ^^;

Я хочу скопировать ввод в буфер обмена, нажав copy1 с помощью js или jquery

Я хочу скопировать текстовую область в буфер обмена, нажав copy2

Спасибо, если дадите мне знать, как.

==============================================

Я искал какой-то код для этого, но это было сложно.

Я применил этот код

$("#copy_code").click(function(e) {

  e.preventDefault();

  document.execCommand('copy', false, document.getElementById('select-this').select());

  alert("copy is completed")
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="" value="{{p.content1}}" size="40">
<button class="btn btn-outline-primary btn-sm myinput" id="copy_code select-this">copy1</button>

ех) https://codepen.io/fabean/pen/GprQJa

но ошибка

Uncaught TypeError: Cannot read property 'select' of null
    at HTMLButtonElement.<anonymous> ((index):251)
    at HTMLButtonElement.dispatch (custom.js:3)
    at HTMLButtonElement.q.handle (custom.js:3)

1 Ответ

1 голос
/ 04 июня 2019

id="copy_code select-this" неверно.Догадываясь о вашем JS, вы на самом деле хотели, чтобы input имел id="select-this", а button имел id="copy_code".

$("#copy_code").click(function(e) {

  e.preventDefault();

  document.execCommand('copy', false, document.getElementById('select-this').select());

  alert("copy is completed")
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="select-this" type="text" name="" value="{{p.content1}}" size="40">
<button class="btn btn-outline-primary btn-sm myinput" id="copy_code">copy1</button>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...