Saturday, November 03, 2007

Focus on an image when HTML page loads

Problem - On a JSP or HTML page, you have a form which has an image as an input element. You need to set focus on image in the form when the page loads.
You cannot normally set focus on an image in a form, using document.forms[0].image.focus(); .  I dont know if it works for you, but it did not work for me.
You can use the W3C DOM to achieve this.  Among other things, the DOM model allows the HTML document to be treated as an XML. It also allows javascript or other scripting languages to parse, read or modify this XML.
The solution is below - (call this method on page body load event)


  function focusButton()
  {
      var inputelements = document.getElementsByTagName("input");
      for (i=0; i<inputelements.length ; i++)
      {
          if (inputelements[i].type=="image" && inputelements[i].name=="submitimg")
          {
              inputelements[i].focus();
              break;
          }
      }
  }

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home