Javascript Form How to - Set focus to the File Upload Input field








Question

We would like to know how to set focus to the File Upload Input field.

Answer


 <!-- w  w w  .ja  va  2s.c o  m-->
<html>
<body>
    <script language="JavaScript">
    function checkFile(){
         document.form1.uploadbox.focus();
         document.form1.textbox.value = "Verify that filename is correct";
    }
    </script>
    <form name="form1">
    Enter Filename:
    <input type="file" name="uploadbox">
    <input type="button" value="Okay" onClick="checkFile()">
    <br><br>
    Confirmation Message:
    <input type="text" name="textbox" size=35>
    </form>
</body>
</html>

The code above is rendered as follows: