select all in every ASP.NET textbox upon focus

Posted on 2011-10-18
Last Modified: 2012-05-12
I love this little bit of jQuery 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.

Question by:bamapie
    LVL 81

    Accepted Solution

    try this :
    $(':text').focus(function() {
        setTimeout(new Function( "$(':text:eq(" +  $(this).index(":text") + ")').select()" ), 50 );

    Open in new window


    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now