How to set text box's text to be selected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilOlMe
    New Member
    • May 2007
    • 74

    How to set text box's text to be selected

    Hi there!

    I was wondering if it is possible to select all of the text in an <input type="text"> element onFocus?

    Thanks!

    -LilOlMe
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    Use the select() method.

    Comment

    • lilOlMe
      New Member
      • May 2007
      • 74

      #3
      Originally posted by acoder
      Welcome to TSDN!

      Use the select() method.
      Thank you Acoder!

      I knew about the select() function before but it wasn't working. I have found something that does work but I'm confused as to why this doesn't work:
      Code:
      function SetWhichTextBoxHasFocus(name)
      {
           document.getElementById('txtBoxWithFocus').value = name;
           document.getElementById(name).select();
      }
      While this does:
      Code:
      function SetWhichTextBoxHasFocus(name)
      {
           document.getElementById('txtBoxWithFocus').value = name;
           document.getElementById('txtBoxWithFocus').select();
      }

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        Originally posted by lilOlMe
        Thank you Acoder!

        I knew about the select() function before but it wasn't working. I have found something that does work but I'm confused as to why this doesn't work:
        Code:
        function SetWhichTextBoxHasFocus(name)
        {
             document.getElementById('txtBoxWithFocus').value = name;
             document.getElementById(name).select();
        }
        While this does:
        Code:
        function SetWhichTextBoxHasFocus(name)
        {
             document.getElementById('txtBoxWithFocus').value = name;
             document.getElementById('txtBoxWithFocus').select();
        }
        you set the value of your textbox 'txtBoxWithFocu s' first, and then you try to get the element ... but the id of it still is 'txtBoxWithFocu s' ... so the second of your lines will not work until you get the element by ID!

        i assume that name != 'txtBoxWithFocu s' :) of course

        kind regards ...

        Comment

        • lilOlMe
          New Member
          • May 2007
          • 74

          #5
          Originally posted by gits
          you set the value of your textbox 'txtBoxWithFocu s' first, and then you try to get the element ... but the id of it still is 'txtBoxWithFocu s' ... so the second of your lines will not work until you get the element by ID!

          i assume that name != 'txtBoxWithFocu s' :) of course

          kind regards ...
          actually .........it does........... because I do:
          Code:
           document.getElementById('txtBoxWithFocus').value = name;
          Or am I wrong here?

          Comment

          • lilOlMe
            New Member
            • May 2007
            • 74

            #6
            Originally posted by gits
            you set the value of your textbox 'txtBoxWithFocu s' first, and then you try to get the element ... but the id of it still is 'txtBoxWithFocu s' ... so the second of your lines will not work until you get the element by ID!

            i assume that name != 'txtBoxWithFocu s' :) of course

            kind regards ...


            Ok I completely don't understand what you're trying to tell me.
            I call the SetWhichTextBox HasFocus here:
            [code=html]
            <input type="text" value="00" maxlength="2" id="ctl00_cph_m ainContent_TPKR _Holiday3_TXT_H rStart" OnFocus="SetWhi chTextBoxHasFoc us('ctl00_cph_m ainContent_TPKR _Holiday3_TXT_H rStart');" />
            <input type="text" value="00" maxlength="2" id="ctl00_cph_m ainContent_TPKR _Holiday3_TXT_M inStart" OnFocus="SetWhi chTextBoxHasFoc us('ctl00_cph_m ainContent_TPKR _Holiday3_TXT_M inStart');" />
            [/code]
            So the name of the text input element that has focus is passed to the SetWhichTextBox HasFocus() function. I do this because I have some buttons that will change the value of a text element depending on which had focus last... since the focus changes to the button when the user clicks it.

            Anyways, so the name of text element that currently has focus is passed to the SetWhichTextBox HasFocus() function.

            Then I set the variable 'txtBoxWithFocu s' to be equal to that name.
            Then I try to select the contents of the text element by using the variable I just set....which is equal to the name I passed in, which is equal to the name of the text element.

            [code=javascript]
            function SetWhichTextBox HasFocus(name)
            {
            document.getEle mentById('txtBo xWithFocus').va lue = name;
            document.getEle mentById('txtBo xWithFocus').se lect( );
            }
            [/code]

            So why does the select work when I use the name parameter passed in, but not the variable that I just set?

            I still don't understand.

            -LilOlMe

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              To set the variable txtBoxWithFocus :
              Code:
              var txtBoxWithFocus = name;
              Then to select:
              Code:
              document.getElementById(txtBoxWithFocus).select()
              though to be honest I don't know why you want to do it like that, unless I've misunderstood.

              Comment

              • lilOlMe
                New Member
                • May 2007
                • 74

                #8
                Originally posted by acoder
                To set the variable txtBoxWithFocus :
                Code:
                var txtBoxWithFocus = name;
                Then to select:
                Code:
                document.getElementById(txtBoxWithFocus).select()
                though to be honest I don't know why you want to do it like that, unless I've misunderstood.
                I've created a control that lets you control time with buttons.
                It lets you set a time period.

                Code:
                Start Time: [textboxHrs][textboxMinutes]{buttons up and down}
                End Time: [textboxHrs][textboxMinutes]{buttons up and down}
                I had to control either the textboxHrs or the textboxMinutes when the user clicked the up or down button. (This is why I had to store the name of the text box, because the text box's focus is lost when the user clicks the button)

                The problem was that the user couldn't really tell which text box the button was going to change.
                Also the user couldn't just click on the text box and enter a time because of the max length restrictions.

                So I've written a little code that selects the text box's contents and stores the name of that text box (to indicate to the button that it should be changed) when it has focus...

                I reselected the text when the user clicked the buttons to make sure the user knew which text box was being changed.

                Its really a cute little control.
                They are added to the web page dynamically through a .NET application.

                I'm sure there's an easier way to solve this problem, but I'm very new to JavaScript so I have to do things the hard way first.

                Now that its working perfectly I'd love to show it off...but I can't post all the code here.

                -LilOlMe

                Comment

                • lilOlMe
                  New Member
                  • May 2007
                  • 74

                  #9

                  Now that its working perfectly I'd love to show it off...but I can't post all the code here.
                  Maybe I'll write an article for you guys showing step by step how to create this control.

                  It has a lot of stuff in it:
                  • uses JavaScript to create a text box mask that only accepts numbers
                  • changes the value of a text box
                  • selects text in text boxes....


                  It'd be really long though...doesn' t fit in one post.

                  -LilOlMe

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    Originally posted by lilOlMe
                    Ok I completely don't understand what you're trying to tell me.

                    ...

                    [code=javascript]
                    function SetWhichTextBox HasFocus(name)
                    {
                    document.getEle mentById('txtBo xWithFocus').va lue = name;
                    document.getEle mentById('txtBo xWithFocus').se lect( );
                    }
                    [/code]

                    So why does the select work when I use the name parameter passed in, but not the variable that I just set?

                    I still don't understand.

                    -LilOlMe
                    i think you probably get it now ... but i want to explain it to you:

                    using:

                    [CODE=javascript]
                    document.getEle mentById('txtBo xWithFocus').va lue = name;
                    [/CODE]

                    sets the value of the element that has the id = 'txtBoxWithFocu s' ... and nothing more. if you would do that with an input-field shown on a html-page you will see that the content of that box is set with this code ... and the content would be the value of your variable name. but of course you have to have an inputbox with an id that is 'txtBoxWithFocu s' ...

                    acoder showed you how to set a variable ... using 'txtBoxWithFocu s' will not work, until that is a simple string ... and getElementById would try to get an element with that string as id.

                    hope this helps to understand?

                    Comment

                    Working...