473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Focus on a text field when the page is loaded

258 100+
Hi everyone
I have a chat form and I need my textfield to be the active one when the page is loaded.

Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post">
  2.      <input type="text" name="text1">
  3.      <input type="text" name="text2">
  4.      <input type="text" name="text3">
  5.      <input type="text" name="text4">
  6.   </form>
  7.  
For example I need to focus on ( text2 ) when the page is loaded... and i dont want the user to click on the textfield to activate it every time he/she wants to type a message
Sep 25 '07 #1
9 1711
dmjpro
2,476 2GB
Hi everyone
I have a chat form and I need my textfield to be the active one when the page is loaded.


Expand|Select|Wrap|Line Numbers
  1. <form action="" method="post">
  2.      <input type="text" name="text1">
  3.      <input type="text" name="text2">
  4.      <input type="text" name="text3">
  5.      <input type="text" name="text4">
  6.   </form>
  7.  

For example I need to focus on ( text2 ) when the page is loaded... and i dont want the user to click on the textfield to activate it every time he/she wants to type a message
Expand|Select|Wrap|Line Numbers
  1. <body onload = setFocus()>
  2. <form action="" method="post" name = "test_form">
  3.      <input type="text" name="text1">
  4.      <input type="text" name="text2">
  5.      <input type="text" name="text3">
  6.      <input type="text" name="text4">
  7.   </form>
  8. </body>
  9.  
Expand|Select|Wrap|Line Numbers
  1. function setFocus()
  2. {
  3.  document.test_form.text2.focus();
  4. }
  5.  
Try this code, it will work for you.
Good Luck :-)

Kind regards,
Dmjpro.
Sep 25 '07 #2
bnashenas1984
258 100+
Thank you friend
It worked and was a big help
You just forgot to write id in the input tag...
It should be like:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="text2" id="text2">
  2.  
Thanks again
Sep 25 '07 #3
acoder
16,027 Expert Mod 8TB
You just forgot to write id in the input tag...
Actually if you have an id, just use:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("text2").focus();
Sep 25 '07 #4
bnashenas1984
258 100+
Yes ... Thanks
Its working now
Sep 25 '07 #5
acoder
16,027 Expert Mod 8TB
Yes ... Thanks
Its working now
I know the first solution worked, but this is an alternative solution using an id rather than the document.form.element notation.
Sep 25 '07 #6
bnashenas1984
258 100+
Thank you
And I think your way is better because the other one seems to have problem with Mozilla...
I usualy test my scripts with both IE and Mozilla and I just realize that the first one is not working in Mozilla or it may be something wrong with my browser?
Sep 25 '07 #7
acoder
16,027 Expert Mod 8TB
Thank you
And I think your way is better because the other one seems to have problem with Mozilla...
I usualy test my scripts with both IE and Mozilla and I just realize that the first one is not working in Mozilla or it may be something wrong with my browser?
It should work though the correct syntax would be:
Expand|Select|Wrap|Line Numbers
  1. document.forms["test_form"].text2.focus();
Sep 25 '07 #8
bnashenas1984
258 100+
Check this one
Its what I'v had problem with for a long time and didn't know what to do with it

Expand|Select|Wrap|Line Numbers
  1. <table id="mytable" width="100" height="100" border="1" onclick="mytable.style.display='none'">
  2.     <tr>
  3.         <td>
  4.  
  5.         </td>
  6.     </tr>
  7. </table>
  8.  
This one works in IE but not in Mozilla.. And you can't even add ( document. )
Sep 25 '07 #9
acoder
16,027 Expert Mod 8TB
Either use "this.style.display" or document.getElementById("mytable").style.display.
Sep 25 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: 'bonehead | last post by:
Greetings, I'm still something of a newbie to html/php/mysql. I have a php/html form with several fields of type "input". If the user enters improper data in a particular field and clicks the...
5
by: Mike Hnatt | last post by:
I'd like the focus to be on "mycontrol" when the page is first loaded. I'm getting an undefined error and am assuming that I can't do this in the body ONLOAD event because the control is not yet...
11
by: JCO | last post by:
I have a text box, on my form that I cant get the focus on. I'm using it as shown: <head> <body onload=document.getPassword.txtPasswordName.focus()> </head> Where getPassword is the form &...
6
by: Csaba | last post by:
I'd like to have the cursor in the login form's username input text field when users load the login page. It works fine with the following implementation: <body...
3
by: Dai Ba Wong | last post by:
Hi: Currently I am having a problem with my webpage. My page consist of two frames, one consist of input text field and the other contains link for different pop-up windows. The problem...
4
by: PK9 | last post by:
I have a button at the top of my page that I have an onClick event handler for which makes some new controls (at the bottom of my page) visible. When the user clicks the button I make the new...
4
by: bienwell | last post by:
Hi, I have a problem and really need your help. In my web page ASPX, I have some text fields to accept data from users. I did form validation like this : <td class="dataTD" style="HEIGHT:...
2
by: Rey | last post by:
Howdy all. Using visual web developer (VB) on xp pro box. My problem with with a web form that on accessing the calendar control causes a postback that moves the cursor back to the txtFirstName...
4
by: bnashenas1984 | last post by:
Hi everyone I have a chat form and I need my textfield to be the current one when the page is loaded. <form action="" method="post"> <input type="text" name="text1"> <input...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.