2 Replies Latest reply on May 27, 2009 9:55 AM by oneworld95

    Set focus on load of page

    oneworld95

      Hi. How do you set focus into a text field when the page loads? I've tried this code but it's not doing anything:

      window.onload = setFocus;
      function setFocus(){
       document.getElementById("metaform:txtAssetID").focus();
      }



        • 1. Re: Set focus on load of page
          mpickell

          Try instead (requires jQuery):

          (function($) {
           $(document).ready(function () {
           $("#metaform\\:txtAssetID").focus();
           }); /* end of document.ready */
          })(jQuery);


          This waits for the page to load, and then sets focus. you version attempts to do it prior to loading (i believe) and therefore the component isn't there yet.

          If you are using other javascript libraries that use the "$", don't forget to add jQuery.noConflict; when loading the jQuery library.

          Although i haven't tried it yet, this script could probably be added directly to the rich:jquery component.

          • 2. Re: Set focus on load of page
            oneworld95

            Thanks, mpickell. I was wondering how to use jQuery and didn't know the syntax. That will do the trick.