focus();

    • Hi All...

      I have a text filed with preloaded text in it. I have the reset button set to foucus on that field when pushed...how can i get it to focus at the beginig of the preloaded text as opposed to after it.

      ie:

      TT <-it curently focuses here

      -->i want it to focus here TT


      code:

      HTML Source Code

      1. <html>
      2. <body>
      3. <form name="f1" method="post" onReset="document.f1.text1.focus();">
      4. <input type="text" name="text1" maxlength="8" value="TT"> <input type="reset" value="clear" >
      5. </form>
      6. </body>
      7. </html>
    • Here is the code for what you're trying to do. Tested on chrome and firefox.

      JavaScript Source Code

      1. <html>
      2. <body>
      3. <form name="f1" method="post" onreset="focusStart(); return false;">
      4. <input type="text" name="text1" maxlength="8" value="TT"> <input type="reset" value="clear" >
      5. </form>
      6. <script>
      7. function focusStart() {
      8. var input = document.getElementsByTagName('input')[0]; // Here you get the input field
      9. if (input.createTextRange) { // Detects if createTextRange function exists
      10. var part = input.createTextRange();
      11. part.move("character", 0);
      12. part.select();
      13. } else if (input.setSelectionRange) { // Detects if setSelectionRange function exists
      14. input.setSelectionRange(0, 0);
      15. }
      16. input.focus();
      17. }
      18. </script>
      19. </body>
      20. </html>
      Display All