Salome Sulaberidze
Salome Sulaberidze

Reputation: 87

How to copy several text to clipboard with jquery

I have got all elements which contains checked checkbox. when click button it copies the text which has checked checkbox to clipboard. my code works well, but it copies only text of one element. I want to copy as much as i check. can someone help me please?

here is my code


  $( ".copy" ).click(function() {
                    $('.LC20lb').filter(':has(:checkbox:checked)').each(function() {                     
                      var inp = $("<input>");
                      $("body").append(inp);
                      inp.val($(this).text()).select();
                      document.execCommand("copy");
                      inp.remove();


                      })

Upvotes: 0

Views: 92

Answers (1)

Anurag Srivastava
Anurag Srivastava

Reputation: 14413

Build a variable in each iteration to get text from all the checkboxes, then use that value to copy:

$( ".copy" ).click(function() {
  var inp = $("<input>");
  $("body").append(inp);
  var str = ""

  $('.LC20lb').filter(':has(:checkbox:checked)').each(function() {                     
     str+= $(this).text()
  })

  inp.val(str).select();
  document.execCommand("copy");
  inp.remove();
})

Upvotes: 1

Related Questions