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>

<body>
<form name="f1"  method="post" onReset="document.f1.text1.focus();">
<input type="text" name="text1"  maxlength="8" value="TT">  <input type="reset" value="clear" >
</form>
</body>
</html>

Comments

  • Here is the code for what you're trying to do. Tested on chrome and firefox.
    <html>
    
    <body>
    <form name="f1"  method="post" onreset="focusStart(); return false;">
    <input type="text" name="text1"  maxlength="8" value="TT">  <input type="reset" value="clear" >
    </form>
    <script>
        function focusStart() {
            var input = document.getElementsByTagName('input')[0]; // Here you get the input field
            if (input.createTextRange) { // Detects if createTextRange function exists
                var part = input.createTextRange();
                part.move("character", 0);
                part.select();
            } else if (input.setSelectionRange) { // Detects if setSelectionRange function exists
                input.setSelectionRange(0, 0);
            }
            input.focus();
        }
        </script>
    </body>
    </html>
    
Sign In or Register to comment.