Results 1 to 3 of 3

Thread: [RESOLVED] How to select all the text in a Textbox in JQuery

  1. #1

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Resolved [RESOLVED] How to select all the text in a Textbox in JQuery

    Good Day All

    i have a Jquery code defined like this



    Code:
           // Let's add it to textarea this time
            $(".cnt").focus(function()
            {
                // Check for the change
                if(this.value == this.defaultValue){
                    this.select();
                }
            });
    and i am creating a textbox on fly and after creating it i bind data to it and after that i want to attach a focus event if there a value "0" on it

    Code:
      If cnt.Text = "0" Then
                cnt.CssClass = "cnt"
            End If

    but still when i select a textbox that has "0" it does not select the whole content of the textbox. i went through a breakpoint and it goes through this line

    Code:
     cnt.CssClass = "cnt"
    Thanks

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: How to select all the text in a Textbox in JQuery

    If the TextBox you're creating is actually created after the page has been loaded, then this won't work just because the focus event you're creating in JavaScript is created when the page loads. Any elements created after that JavaScript has been executed won't have that focus event attached to it. As long as you're using jQuery >= 1.4.1, you can use jQuery's live() event attachment function, however, which will attach an event to your selector to any elements that exist as it's called, as well as any elements that match that selector in the future.

    Here's an example:
    Code:
    $('.cnt').live('focus', function(){
      // Check for change
      if(this.value == this.defaultValue){
        this.select();
      }
    });
    Hope that helps. If it doesn't, please provide a bit more information and possibly a bit more code to better describe what you're trying to do.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Re: How to select all the text in a Textbox in JQuery

    Thanks guys for the reply, to resolve this i applied this to all the textboxes like this

    Code:
      $(document).ready(function() 
        {
             $("input:text").focus(function() 
             { 
             $(this).select(); 
             } 
             );
         });

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width