Rushi Jagani
Rushi Jagani

Reputation: 160

Jquery how to copy input field value on clipboard automatically after ajax success response

What I achieved is I can copy link on click event by using Clipboard.js but I also want to copy an input field value on the clipboard automatically after an ajax success response. How to achieve this functionality?

Upvotes: 0

Views: 805

Answers (1)

Sankar
Sankar

Reputation: 7107

You can use document.execCommand('copy')

function copyText() {
  var input1 = document.getElementById('txt');
  input1.select();
  document.execCommand('copy')
}
<input id='txt' required />

<input type="submit" value="copy" onclick="copyText()" />
<br>
<br>
<textarea placeholder="Paste it here"></textarea>

For your req:

(From the fiddle you've mentioned in comment)

$.ajax({
   type: 'POST',
   url: url,
   async: false,
   data: data
   success: function(response) {
     $("#shortlink").val(response);
     $('#shortlink').select();
     document.execCommand('copy')
   },
   error: function(textStatus, errorThrown) {

   }
 });

Upvotes: 1

Related Questions