6 Replies Latest reply on Nov 20, 2007 8:13 PM by lowroller

    .setFocus() Doesn't Work

      I am calling txtUserId.setFocus(); when the application loads. Why is the cursor not being placed in the text box?

      If I refresh the page or change view state back to the original view state and I call the code it again, it works. Am I missing something simple, or is this a bug? Thanks!
        • 1. Re: .setFocus() Doesn't Work
          atta707 Level 2
          Which exact event handler you call your code in?

          calling the method in callLater might help.

          • 2. Re: .setFocus() Doesn't Work
            levancho Level 3
            problem is that first you have to make sure flash object(hosting your flex app) has focuse from html perpective, and then your setFocuse will work .once you click on flesh object I bet your input it gets focuse(regardles where you click inside flash object), (that is why it works second time) .
            • 3. Re: .setFocus() Doesn't Work
              lowroller Level 1
              I am using the following code:

              creationComplete="initApp()" - inside mx:Application tag

              private function initApp():void {
              currentState = 'Login';
              txtUserId.text = '';
              txtPassword.text = '';

              It seems levancho is correct. If I click anywhere on the Flash object, the focus is set to the text box. How do I overcome this? Could you provide sample code?

              Thanks to both of you for your replies!
              • 4. Re: .setFocus() Doesn't Work
                levancho Level 3
                if its even possible it has to be done on html wrapper (javascript ) level, i have not tried it myself, but its interesting task will try to get it working this afternoon, I also need it, even though its not show stopper for me.
                • 5. Re: .setFocus() Doesn't Work
                  ntsiii Level 3
                  I don't have any code handy, but there are javascript examples out there.

                  Major caveat: This method will not work with FireFox. There is no current solution that works with FireFox.

                  • 6. Re: .setFocus() Doesn't Work
                    lowroller Level 1
                    OK...With all the comments I was able to figure it out.

                    In the JavaScript in the wrapper file, I added the following function:

                    function startup() {
                    document.CGBSupport.focus(); //CGBSupport is the ID in the existing JavaScript ("id", "CGBSupport")

                    and in the HTML in the wrapper file, I added the following "onload" statement:

                    <body scroll="no" onload="startup()">

                    Thanks to all who replied!