[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

select all in every ASP.NET textbox upon focus

I love this little bit of jQuery code...in IE.

$(document).ready(function() {

   // Add this behavior to all text fields
   $('input[type=text]').focus(function() {
      // Select field contents

For the page I'm working on, I really like this behavior.  If you select a textbox (INPUT type=text), this bit of code selects all text in that box, ready for you to delete it or replace it, whatever.

Problem is, this doesn't work in Firefox.  How do I modify it so that it works in both IE and Firefox?

I'm only interested in meta-code like this, run it once and that's it.  No adding code per-each-control.  Don't want that.  This is behavior I want attached to every single enabled textbox on my page.  Ideally, I just want to modify this bit of code to make it work.

1 Solution
try this : http://jsfiddle.net/tzpay/
$(':text').focus(function() {
    setTimeout(new Function( "$(':text:eq(" +  $(this).index(":text") + ")').select()" ), 50 );

Open in new window

bamapieAuthor Commented:
I'm a dope.  Still not getting this in Firefox.  I stuck an alert() at the end of your code that you're using in that setTimeout(), and I never see it in FF but do in IE.

But you know, it may be a 3rd-party tool I'm using that prevents non-numeric text entry in this textbox.

But then, you'd think if it's interfering, it would interfere with IE too, not just FF.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now