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:
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.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>
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
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
toCode:<textarea onkeypress="ifEnter(this,event);" name="username" rows="10" cols="40"> </textarea>
then your code at the bottomCode:<textarea onkeypress="ifEnter(this,event);" name="username" id="username" rows="10" cols="40"> </textarea>
Will work with no problemsCode:document.getElementById("username").focus();
Sorry, fixed.
Thanks!Will work with no problems
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
Glad to see you got it fixed![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks