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

    Set focus on load of page

    Alex Cougarman Apprentice

      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
          Matthew Pickell Newbie

          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
            Alex Cougarman Apprentice

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