![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
I want to make a form field take focus ideally coded in the form :- form method="post" action="index.php?action=newname" input type="text" name="name" size="40" value="" / input type="submit" name="submit" value="Create" / /form |
#2
| |||||
| |||||
|
|
Aaron Gray wrote: I want to make a form field take focus ideally coded in the form :- form method="post" action="index.php?action=newname" * * <input type="text" name="name" size="40" value="" / * * <input type="submit" name="submit" value="Create" / /form This is how I'd do it, using javascript: (I know of no simple HTML solution) |
|
1. Add a name to your form: <form name=myform method=... |
|
2. Add the following to give focus to your "name" field: SCRIPT>document.myform.name.focus()</SCRIPT |
|
In place of "document" you can use "this" if you prefer, because your script is inside the document, so "this" knows where to operate. |
|
The [name] in the script is the ["name"] from your type="text" control; nothing to do with the "name" in "name=" found in both your controls. So if you'd used [name="person"] your script would be document.myform.person.focus(). The references are case sensitive, so neither "Myform" nor "Name" would work in your script. I don't know if there are any rules on where the SCRIPT should go. I put it just after the form, or even inside the form. |
#3
| |||
| |||
|
|
Best practice is to do: body onLoad="document.forms[0].elements['myname'].focus()" because onLoad is explicitly executed just after the whole page is loaded. |
#4
| |||
| |||
|
|
Steve Swift wrote: 2. Add the following to give focus to your "name" field: SCRIPT>document.myform.name.focus()</SCRIPT |
|
[...] In place of "document" you can use "this" if you prefer, because your script is inside the document, so "this" knows where to operate. I would always use 'document' (but that might be my personal preference) |
|
I don't know if there are any rules on where the SCRIPT should go. I put it just after the form, or even inside the form. Best practice is to do: body onLoad="document.forms[0].elements['myname'].focus()" because onLoad is explicitly executed just after the whole page is loaded. |
#5
| |||
| |||
|
|
Steve Swift wrote: 2. Add the following to give focus to your "name" field: SCRIPT>document.myform.name.focus()</SCRIPT |
|
[...] In place of "document" you can use "this" if you prefer, because your script is inside the document, so "this" knows where to operate. I would always use 'document' (but that might be my personal preference) |
|
I don't know if there are any rules on where the SCRIPT should go. I put it just after the form, or even inside the form. Best practice is to do: body onLoad="document.forms[0].elements['myname'].focus()" because onLoad is explicitly executed just after the whole page is loaded. |
#6
| |||||
| |||||
|
|
[this supersedes my previous followup] |
|
Bart Van der Donck wrote: Steve Swift wrote: 2. Add the following to give focus to your "name" field: SCRIPT>document.myform.name.focus()</SCRIPT The required `type' attribute is missing. |
|
I don't know if there are any rules on where the SCRIPT should go. I put it just after the form, or even inside the form. Best practice is to do: body onLoad="document.forms[0].elements['myname'].focus()" because onLoad is explicitly executed just after the whole page is loaded. Very true. Whereas `onLoad' should be `onload', as it is better for several reasons I already mentioned before. |
|
And the reference worm should be at least shortened; |
|
it would be necessary to determine whether focus() would be successful. |
#7
| ||||
| ||||
|
|
Thomas 'PointedEars' Lahn said the following on 1/22/2008 7:09 AM: Bart Van der Donck wrote: Steve Swift wrote: 2. Add the following to give focus to your "name" field: SCRIPT>document.myform.name.focus()</SCRIPT The required `type' attribute is missing. That's nice[1]. Irrelevant as well. |
|
snipped useful information I don't know if there are any rules on where the SCRIPT should go. I put it just after the form, or even inside the form. Best practice is to do: body onLoad="document.forms[0].elements['myname'].focus()" because onLoad is explicitly executed just after the whole page is loaded. Very true. Whereas `onLoad' should be `onload', as it is better for several reasons I already mentioned before. There is no difference in the two when it is in HTML code. I have already explained that to you. No difference what-so-ever. Just something you seem to prefer and want everybody else to do it Thomas' way. It can be anything from onload to ONLOAD and as long as it is spelled correctly, no browser *can* care because HTML is case insensitive. |
|
And the reference worm should be at least shortened; "Reference worm"? Is that another Thomas-word? |
|
it would be necessary to determine whether focus() would be successful. True, and we know that is impossible [...] |
#8
| |||||
| |||||
|
|
Randy Webb wrote: Thomas 'PointedEars' Lahn said the following on 1/22/2008 7:09 AM: Bart Van der Donck wrote: Steve Swift wrote: 2. Add the following to give focus to your "name" field: SCRIPT>document.myform.name.focus()</SCRIPT The required `type' attribute is missing. That's nice[1]. Irrelevant as well. It isn't, stupid. |
|
snipped useful information I don't know if there are any rules on where the SCRIPT should go. I put it just after the form, or even inside the form. Best practice is to do: body onLoad="document.forms[0].elements['myname'].focus()" because onLoad is explicitly executed just after the whole page is loaded. Very true. Whereas `onLoad' should be `onload', as it is better for several reasons I already mentioned before. There is no difference in the two when it is in HTML code. I have already explained that to you. No difference what-so-ever. Just something you seem to prefer and want everybody else to do it Thomas' way. It can be anything from onload to ONLOAD and as long as it is spelled correctly, no browser *can* care because HTML is case insensitive. I have pointed out before that HTML is not per se case-insensitive. |
|
Regardless, I have explained why it is not merely my preference that makes this suggestion a sound one. But you are too stupid to read, let alone comprehend what was written. |
|
And the reference worm should be at least shortened; "Reference worm"? Is that another Thomas-word? It is yet another thing that you fail to understand. |
|
it would be necessary to determine whether focus() would be successful. True, and we know that is impossible [...] If you know that it is impossible, you have some unlearning to do, because that is wrong. |
#9
| |||
| |||
|
|
It is easy to prove me wrong though. Post a script that will determine if an arbitrary form element can be focused or not. input type="text" onfocus="this.blur()" name="inputYouCantFocus" |
|
Happy scripting! |

#10
| |||
| |||
|
|
Randy Webb a écrit : It is easy to prove me wrong though. Post a script that will determine if an arbitrary form element can be focused or not. input type="text" onfocus="this.blur()" name="inputYouCantFocus" There is too many way allowing us to hide and/or prevent the focus on an element, let me add a few to the list. |
|
1) the obvious <input type="hidden" 2) <input disabled="disabled" 3) <input style="opacity:0" 4) <input style="position:absolute;clip:rect(0,0,0,0)" 5) div style="overflow:hidden;width:0;height:0" input /div 6) div style="position:absolute;z-index:1;width:300px;height:100px" input style="width:100px" /div div style="position:absolute;z-index:2;width:300px;height:100px; background-color:red"></div 7) with this one, the focus can occur only with a mouse. You got a keyboard only, you are screwed and can't focus it at all. How a script would be able to determine if the second input is focusable ? input input tabindex="-1" and many many more tricky ways to hide anything we want. |
![]() |
| Thread Tools | |
| Display Modes | |
| |