Results 1 to 4 of 4

Thread: Having problem with focus()

  1. #1
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having problem with focus()

    I have used the following code to force cursor into the first field of a form but it doesn't work for some reason. The code does not flag any errors but it displays the form with no visible cursor. You have to click in the first field of the form to get the cursor. Can anyone suggest why?

    Code:
    <html>
    <head>
    <title>Data Entry Form</title>
    </head>
    
    <body onload="document.dataform.siteName.focus();">
    
    <center>
    <br /><br /><br /><br />
    <table width="365px"  height="200px" cellpadding="10" style="border: 1px solid #CC0000;">
    
    <form name="dataform" method="POST" action="addsite.php">
    
      <tr><td>
       Site Name:
      <INPUT type="text" name="siteName" size="40"> <br />
      Username:
      <INPUT type="text" name="user" size="10"> <br />
      Password:
      <INPUT type="text" name="password" size="20"> <br />
    </td></tr>
      
      <INPUT type="hidden" name="siteName" value="<?php echo $_POST['siteName']; ?>">
      <INPUT type="hidden" name="user" value="<?php echo $_POST['user']; ?>">
      <INPUT type="hidden" name="pass" value="<?php echo $_POST['password']; ?>">
      <tr><td align="center">
      <INPUT type="submit" value="Add to database" name="Button1">
      <INPUT type="RESET" value="Reset">
      </td></tr>
    
    </form>
    </table>
    </center>
    </body>
    </html>
    Thanks
    Last edited by Snookerman; 08-16-2009 at 05:17 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try this:
    Code:
    <html>
    <head>
    <title>Data Entry Form</title>
    </head>
    
    <body onload="document.getElementById('siteName').focus();">
    
    <center>
    <br /><br /><br /><br />
    <table width="365px"  height="200px" cellpadding="10" style="border: 1px solid #CC0000;">
    
    <form name="dataform" method="POST" action="addsite.php">
    
      <tr><td>
       Site Name:
      <INPUT type="text" name="siteName" id="siteName" size="40"> <br />
      Username:
      <INPUT type="text" name="user" size="10"> <br />
      Password:
      <INPUT type="text" name="password" size="20"> <br />
    </td></tr>
      
      <INPUT type="hidden" name="siteName" value="<?php echo $_POST['siteName']; ?>">
      <INPUT type="hidden" name="user" value="<?php echo $_POST['user']; ?>">
      <INPUT type="hidden" name="pass" value="<?php echo $_POST['password']; ?>">
      <tr><td align="center">
      <INPUT type="submit" value="Add to database" name="Button1">
      <INPUT type="RESET" value="Reset">
      </td></tr>
    
    </form>
    </table>
    </center>
    </body>
    </html>
    It's because you have 2 name attributes with the value of siteName.
    Jeremy | jfein.net

  3. #3
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok,... the duplicate name="siteName" was the problem.
    Thank you very much once again.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help you! Your welcome!

    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •