Closed Thread
Results 1 to 4 of 4

Thread: Textbox focusing?

  1. #1
    Join Date
    Jan 2010
    Location
    Romania, Baia Mare
    Posts
    17
    Rep Power
    0

    Textbox focusing?

    I've been going round in circles on Google trying to find a way to focus
    a textbox when the page loads.
    Here's what my script looks like:

    Code:
    <script type="text/javascript">           
    function ifEnter(field,event) {
    var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (theCode == 13){
    document.forms[0].submit();
    return false;
    } 
    else
    return true;
    }      
    </script>
    
    
    
    <form name="form1" action="<?=$_SERVER['PHP_SELF'] . '#bottom'?>" method="post">
    <p>Type your message here:<br />
    <textarea onkeypress="ifEnter(this,event);" name="username" rows="10" cols="40">
    </textarea><br />
    <input type="submit" name="submituser" value="Send Message" />
    <input type="text" name="fname" />
    <input type="submit" name="submitname" value="Change Name" />
    
    </p>
    </form>
    
    Welcome <?php echo $_SESSION["fname"]; ?>!<br />
    <br />
    <br />
    <br />
    <a name="bottom">
    
    <script language="JavaScript">
    <!--
    
    document.getElementById('username').focus();
    
    //-->
    </script>
    Before it there's a bunch of HTML/PHP and the HTML closers after. None of them have (to my knowledge) anything to do with cursor focusing that they could make it not work.
    I also tried document.username.username.focus(); but nothing happened then, either. And document.form1.username.focus (along with getelementbyid) just in case having two elements with the same name would've been the problem.
    Last edited by blackriderrom; 01-13-2010 at 04:17 AM. Reason: Reword

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: Textbox focusing?

    Please use code tags when post code (highlight your code and press the # button)

    Your problem is that you are using getElementById but your username field doesn't have an id only a name.

    change
    Code:
    <textarea onkeypress="ifEnter(this,event);" name="username" rows="10" cols="40">
    </textarea>
    to

    Code:
    <textarea onkeypress="ifEnter(this,event);" name="username" id="username" rows="10" cols="40">
    </textarea>
    then your code at the bottom
    Code:
    document.getElementById("username").focus();
    Will work with no problems

  4. #3
    Join Date
    Jan 2010
    Location
    Romania, Baia Mare
    Posts
    17
    Rep Power
    0

    Re: Textbox focusing?

    Quote Originally Posted by Feral View Post
    Please use code tags when post code (highlight your code and press the # button)
    Sorry, fixed.

    Will work with no problems
    Thanks!

    I have another question, however:
    The script's job is basically to provide IM-like functionality (so, the textarea should always be focused and enter needs to work as the input key).
    I managed to get enter to work as the input and after changing the script with what you suggested and the page automatically focuses the textarea, but this only happens once.

    The textarea's action is on the same page as the text, so the page reloads every time you submit content; however, this seems to work differently than a normal page load, and I can't figure out how to get the page to automatically focus on the textarea each time it reloads due to form submission rather than an URL being typed in the browser.

    Edit: Problem fixed! The fact that it was using <?=$_SERVER['PHP_SELF'] . '#bottom'?> as the action was the problem. After making it not point to #bottom, it worked perfectly.
    Last edited by blackriderrom; 01-13-2010 at 04:45 AM. Reason: Fixed

  5. #4
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: Textbox focusing?

    Glad to see you got it fixed

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Can't take value from textbox
    By socheata in forum PHP Development
    Replies: 6
    Last Post: 09-24-2010, 03:17 AM
  2. Textbox Value
    By Bioshox in forum PHP Development
    Replies: 6
    Last Post: 02-27-2010, 02:11 PM
  3. 3 variable and textbox
    By bordino in forum Visual Basic Programming
    Replies: 4
    Last Post: 12-30-2009, 06:35 PM
  4. Autocomplete Textbox in C#
    By alipalang in forum C# Programming
    Replies: 1
    Last Post: 10-13-2009, 05:02 PM
  5. Textbox question
    By deas in forum Visual Basic Programming
    Replies: 6
    Last Post: 03-15-2009, 03:15 PM

Tags for this Thread

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