Forum Moderators: open

Message Too Old, No Replies

html form, getting the cursor to start in the first box

         

derek mcgilvray

7:57 am on Jun 15, 2007 (gmt 0)

10+ Year Member



Hi folks,

Rather than my users having to click in the first input box, is there any way to have the cursor automatically start in that box? Thanks for any help,

Derek

birdbrain

8:08 am on Jun 15, 2007 (gmt 0)



Hi there derek_mcgilvray,

does this help...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
window.onload=function() {
document.forms[0][0].focus();
}
</script>

</head>
<body>

<form action="#">
<div>
<input type="text"/>
</div>
</form>

</body>
</html>

birdbrain

derek mcgilvray

10:43 am on Jun 15, 2007 (gmt 0)

10+ Year Member



Thanks for your help Birdbrain. I have 4 forms linking to each other, carrying variables across. Using your help in the post above, it worked for the first form, bringing up a flashing cursor in the first box, but for none of the other three. I used exactly the same code on each page. Any ideas?

birdbrain

11:26 am on Jun 15, 2007 (gmt 0)



Hi there derek_mcgilvray,

I am not quite sure what you mean.

Do you have four forms on one page or one form on four pages.

Would it be possible to see some relevant code?

birdbrain

derek mcgilvray

12:03 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



Yes,
I have four forms which pass variables between them.

<form action="form2.php" method="post">
<input type="hidden" name="usid" value="usid">
<input type="text" name="answer" value="">
</form>

<form action="form3.php" method="post">
<input type="hidden" name="usid" value="usid">
<input type="text" name="answer" value="">
</form>

<form action="form4.php" method="post">
<input type="hidden" name="usid" value="usid">
<input type="text" name="answer" value="">
</form>

I'd like the cursor to be in place ready for the user to start typing, rather than click in the box and then start typing. The code you gave me above worked for the first form but not the others. Has this made it clearer?

Derek

birdbrain

12:39 pm on Jun 15, 2007 (gmt 0)



Hi there derek_mcgilvray,

You can, of course, only have focus on one element at a time.
This example will change focus to the next element onblur of the previous...


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
var df=document.forms;
window.onload=function() {
df[0][1].focus();

df[0][1].onblur=function() {
df[1][1].focus();
}
df[1][1].onblur=function() {
df[2][1].focus();
}
df[2][1].onblur=function() {
df[3][1].focus();
}
}
</script>

</head>
<body>

<form action="form1.php" method="post">
<input type="hidden" name="usid" value="usid">
<input type="text" name="answer" value="">
</form>

<form action="form2.php" method="post">
<input type="hidden" name="usid" value="usid">
<input type="text" name="answer" value="">
</form>

<form action="form3.php" method="post">
<input type="hidden" name="usid" value="usid">
<input type="text" name="answer" value="">
</form>

<form action="form4.php" method="post">
<input type="hidden" name="usid" value="usid">
<input type="text" name="answer" value="">
</form>

</body>
</html>

birdbrain

derek mcgilvray

12:51 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



Thanks Birdbrain, that's it solved - you're a star!

Thanks again

birdbrain

1:01 pm on Jun 15, 2007 (gmt 0)



No problem, you're very welcome. ;)