Search code examples
javascriptjavahtml

struts focus on field after validation without js


i'm using struts (form) + validation i just want to ask if can i set focus on some field on form after validation without javascript? i think i red something in book programming jakarta struts but i can't remember.

thanks


Solution

  • You cannot set focus on a certain field with pure HTML. The tabindex idea as suggested by Bozho is nice, but it will only work if you actually press tab for the first time. It has however the disadvantage that it changes the tabbing order of the input elements. Not really user friendly.

    You'll really need to grab JavaScript for this. Just do something like:

    window.onload = function() {
       document.formname.${inputname}.focus();
       // or:
       document.getElementById(${inputid}).focus();
    };
    

    ...where ${inputname} dynamically resolves to name of the input field as in <input name="foo"> and where ${inputid} resolves to ID of input field as in <input id="foo">. That's all.