Log in

View Full Version : How do you set focus on a specific field?



Dance10Looks10
09-03-2005, 04:11 PM
I'm trying to set focus on the initial form field when the User enters the page. I'd also like to know, in case of an error, how to set focus on the error field.

Can anyone help?

Thanks :)

Twey
09-03-2005, 06:50 PM
<body onload="document.forms[0].elements[0].focus();">

Dance10Looks10
09-03-2005, 10:34 PM
Thanks so very much! I've had open heart surgery and one of the side effects of the heart/lung machine is that parts of memory disappear - forever. This was one of those items. I really appreciate your help!!!

Am assuming, in case of error, that one would canabalize the ONLOAD code and use the apppropriate index, right?

Twey
09-04-2005, 11:56 AM
Well... that's probably one of the most extreme reasons for lack of Javascript knowledge I've ever seen :)
Yes indeed... the code I used was "the first element of the first form on the page." You can change them to reflect your element, or assign it an ID and access it with document.getElementById('id').

mwinter
09-04-2005, 12:34 PM
You can change them [the indices] to reflect your element, or assign it an ID and access it with document.getElementById('id').The forms and elements collections work just as well with id attributes as they do with indices and name attributes, and are better supported than the getElementById method.

Mike

Twey
09-04-2005, 01:25 PM
So, better to use the former than the latter. However, if you add a new form or element before the initial element, you will need to adjust the indices.

Dance10Looks10
09-07-2005, 05:59 AM
You wrote:

"Well... that's probably one of the most extreme reasons for lack of Javascript knowledge I've ever seen " is rather an unkind thing to say. It isn't lack of knowledge,sir. I've taught this stuff at the graduate level for a many years

Talk to any cardiologist and they'll tell you the same thing: open heart surgery using a heart/lung machine selectively destroys parts of memory. Forever. No one knows why.

Perhaps I'm just tired right now but I think an apology would not be out of place here. If I knew to whom this kind of thing should be reported, that would be my next step. It must be very nice to live in your particular world.

Twey
09-07-2005, 08:58 AM
I'm sorry, it wasn't intended to be insulting. Perhaps "memory" is a better word than "knowledge" in the circumstance. If you feel it should be reported, ddadmin is the one to go to.

ddadmin
09-07-2005, 11:32 PM
I think this is just a misunderstanding. From how I saw it, Twey was being playful when he made that comment, with no intent to insult at all. Adding a smilie face at the end usually is a sign of that. :)

Dance10Looks10
09-08-2005, 12:37 AM
OK, then. Over and done with, I guess.