Problem recognizing this.id in jQuery

Asked

Viewed 82 times

0

I have a code below in Javascript that picks up the td that was clicked and copied to the clipboard, but it is not working. The error you see on the console is:

(test). select(); is not a Function

The alert(teste) returns the number of id right. What I need is to copy this id for clipboard clipboard.

$("td").click(function(e){
            var teste = e.target.id;
            alert(teste);

            (teste).select();

             document.execCommand('copy');
               alert('Texto copiado para a área de transferência');

        });
  • To do this, you need to put the text in a textarea.

  • Create a small textarea and make it transparent with css

  • Change to $(teste).select();

  • But even if I put a textarea it returns the error (test). select(); is not a Function

  • I left it : <td><textarea id="2">test</textarea></td>

  • And my jquery looked like this:$("td textarea"). click(Function(e){ var test = $(this). text(); Alert(test); $(test). select(); Document.execCommand('copy'); Alert('Copied text; });

  • would not be teste.select(); ??

  • test.select() Error: .teste.select() is not a Function...

  • var test = $(this). text(); Alert(test); (this). select(); Document.execCommand('copy');

  • I believe you need to copy the element itself and not the value. Do as colleagues have said, copy the TD text to the textarea and do the "var test = $('#2')", your text being and see if it works.

Show 5 more comments

3 answers

1

You can do it this way, without the need to use the id, using the objects Range and Selection javascript:

$("td").click(function(e){
//   var teste = e.target.id;
//   alert(teste);

   var range = document.createRange(); // cria o objeto range
   range.selectNodeContents(this);   // adiciona o conteúdo do elemento clicado
   var sel = window.getSelection();  // cria o objeto Selection
   sel.removeAllRanges(); // limpa o objeto Selection
   sel.addRange(range); // seleciona o texto

   document.execCommand('copy');
   alert('Texto copiado para a área de transferência');

});

1

To get an id of a specific element through jQuery has a slightly better approach instead of using Event, what you do is take it through the command

$(this).attr('id');
So it will take the clicked element in this case and take the id attribute of that element. In the case of . select() you will have to catch him using also the $, would be about that:

$(teste).select();

I hope I’ve helped

1

You need to put the text in a TextArea first, and then do the .select() in the element itself, and not in the value that will be copied.

First place an invisible Textarea on your page:

<textarea id="texto" style="display:none;"></textarea>

So, in your Function:

$("td").click(function(e){
    var valor = e.target.id;        

    $('#texto').val(valor).select();        
    document.execCommand('copy');
    alert('Texto copiado para a área de transferência');
 });

See another example here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.