focus

Jack Michaelson's picture

He has: 1,733 posts

Joined: Dec 1999

He every-1,

I try the following:

Code Sample:

...
<body onload="document.ok.focus()">
...
<input type="button" name="ok" value="  OK  ">
...

As you'll propably understand, I try to get the focus on the OK-button when the page loads. It won't work.
When I make the button a textfield it DOES work.
Does the focus only work on textfields or am I doing sth wrong?

Shakespeare: onclick || !(onclick)

They have: 89 posts

Joined: Sep 1999

Hello, try this.

<FORM NAME="myFormName">
<INPUT TYPE="text" NAME="myTextField">
<input type="button" name="ok" value="ok"
</FORM>

<SCRIPT LANGUAGE="JavaScript"><!--
document.myFormName.ok.focus();
//--></SCRIPT>

Jack Michaelson's picture

He has: 1,733 posts

Joined: Dec 1999

RC-Thanx 4 reply.

But it won't work either (IE4)

They have: 89 posts

Joined: Sep 1999

Yuck.

It works with NS 4.7 and IE 5..I always forget about those pesky older browsers.

They have: 76 posts

Joined: Apr 1999

Jack,

I think the only way to give the button focus in older browsers is to click it.

So maybe:

<body OnLoad="document.form.button.click();return false">

The problem with this is it will run any script/page programmed to run when the button is clicked so I guess it's kinda useless, oh well...

Later,
PJ

They have: 5,633 posts

Joined: Jan 1970

That worked for me in IE4. Check you have the Form Elements names written correctly.

------------------
http://go.to/hass

Jack Michaelson's picture

He has: 1,733 posts

Joined: Dec 1999

Thanx every-one 4 reply,

lloydhass-the elements were named correctly.
I did get it to work by finding out that when I do this...

Code Sample:

&lt;script language="javascript"&gt;
&lt;!--
document.form.ok.focus();
//--&gt;
&lt;/script&gt;
...
&lt;form name="form"&gt;
&lt;input type="button" name="ok"&gt;
&lt;/form&gt;

..it won't work.

But when I do this...

Code Sample:

&lt;form name="form"&gt;
&lt;input type="button" name="ok"&gt;
&lt;/form&gt;
...
&lt;script language="javascript"&gt;
&lt;!--
document.form.ok.focus();
//--&gt;
&lt;/script&gt;

... it DOES work !

That strange order thingy !

[This message has been edited by Jack Michaelson (edited 22 December 1999).]

[This message has been edited by Jack Michaelson (edited 22 December 1999).]

Shakespeare: onclick || !(onclick)

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.