Forum Moderators: open

Message Too Old, No Replies

Placing a cursor in a form

         

fintan

11:26 am on Sep 13, 2002 (gmt 0)

10+ Year Member



How do you place a curser in a form?
Any help would be great.
Thanks

fintan.

Nick_W

11:30 am on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is how I do it...

<body onload="document.forms.NameOfForm.n.focus()">

But I don't understand it ;) I'm not really a JS guy...

Nick

korkus2000

11:32 am on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you asking how to change the mouse cursor or making a text box active?

If you want a textbox to have focus the it is:

document.formName.textBoxName.focus();

BlobFisk

1:48 pm on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Putting korkus2000's code into an onLoad command in the <BODY> tag will mean that the cursor is automatically in the textarea when the page loads on the users machine...

weisinator

5:40 pm on Sep 13, 2002 (gmt 0)

10+ Year Member



You can do it if you want, but keep this in mind:

My bank's site uses this. The script executes after all the images (and Flash animation telling me I should refinance my home: 180K download) have loaded. I don't wait for everything to load. I go on "auto-pilot" when I do my on-line banking, and I'm sure others do, too.

I click the text field, and begin typing my username and password. I'm there for a reason, and I use the web since it's faster than listening to a phone menu.

It's annoying when I type my username, hit tab, and begin to type my password, and half the password is in front of my username when I get ready to submit. (Or worse, I hit "enter" after the "deadline" and it tells me it's incorrect after submission.) I then have to delete that little snippet of password, then re-type my password in the correct field. Extra un-needed step.

Yes, I could disable Javascript, but the site uses it to check my entries prior to submission, and the "Submit" button isn't a submit button, but a normal form button with an onclick() handler (validates format prior to submission, IE dollars and cents, no extraneous characters). Submission of my transfers, etc. happens from Javascript. So disabling javascript would not work in this case.

Of course, I could wait the 30 seconds for the whole page to load and place the cursor there for me, but by then the web loses the speed advantage. I might as well have used the phone in the first place. :)

txbakers

6:18 pm on Sep 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that nifty focus() method should be used when there aren't a ton of things to load. I've had that happen to me as well and it is annoying.

Works great on login pages though.

Purple Martin

11:21 pm on Sep 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A good example of a page where the focus() method works well is Google. The whole page is so small it downloads almost instantly, so there's no noticable delay for the cursor going into the text field.

I've just thought of a way to reduce the pause for larger pages. You don't have to use the body onLoad event to use the focus() method. Instead, you could put the focus() method inside script tags just after the field. This way, the method is called just after the browser renders the field but while the rest of the page, images etc are still downloading.
Something to watch out for: you might need to put your script after the </form> tag to make sure the form has been rendered before the script executes, and if the form is inside a table I'd imagine you'd need to put the script after the </table> tag for the same reason.

fintan

9:18 am on Sep 17, 2002 (gmt 0)

10+ Year Member



Thanks for the advise, Sorry about answering late but I've been out of the loop for a while.

Thanks again.

fintan

bretzke

8:53 pm on Sep 23, 2002 (gmt 0)

10+ Year Member



I have done the focus() and it works fine...but I want to take it to the next level. I need to place the focus at the end of the value of a textbox...the default always puts the cursor at the beining of the textbox no matter if the textbox has a value or is null.

Any ideas?

txbakers

10:44 pm on Sep 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



style="text-align:right"

just add that to the <input> and it should work.