1

I'm trying to create a button linked to a text field that will let me select all the text in a textarea so the user can copy it to the clipboard. I've tried this:

$('#selectCode').click(function() {
     var input = $('#MyTextBox');
     input.focus();
     input.select();
});

based on several examples I found online where you focus on the input and then select it. But this doesn't seem to work in jQuery -- at least the way I'm doing it. Can someone help?

5
  • 1
    This code works: jsbin.com/imekez. Perhaps something else on your page is interfereing? Commented Mar 13, 2012 at 16:04
  • I usually use pure javascript for this. Pretty much the same thing just replacing the $("#MyTextBox"); to document.getElementById('MyTextBox'); Commented Mar 13, 2012 at 16:06
  • These must be something else on the page interfering. I tried the code below and it doesn't work either. Ok, thanks, guys. Commented Mar 13, 2012 at 16:07
  • possibly include the html snippets around the textbox and button? Might help. Commented Mar 13, 2012 at 16:13
  • There was a typo in the id of the input. Thanks for all the help! Commented Mar 13, 2012 at 16:15

1 Answer 1

2
$(function(){
   $("#selectCode").click(function(){
     $("#MyTextBox").select();       

   });
});

Here is the working demo : http://jsfiddle.net/vqGM4/3/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.