Error: document.getElementById("ff") has no properties

L

linuxnooby

Hi

I want a form field to be selected when the page loads.

But I get the error message
Error: document.getElementById("ff") has no properties

any ideas what I am doing wrong?

code below
cheers Dave


<script type="text/javascript">
function setfocus()
{
document.getElementById('ff').focus();
}
window.onload = setfocus();
</script

<form>
<input type="text" id="ff" >
</form>
 
R

RobG

Hi

I want a form field to be selected when the page loads.

But I get the error message
Error: document.getElementById("ff") has no properties

any ideas what I am doing wrong?

Passing the result of setfocus(), rather than a reference (see below).
code below
cheers Dave


<script type="text/javascript">
function setfocus()
{
document.getElementById('ff').focus();

Nothing to do with your problem, but it is considered better to use the
forms collection rather than getElementById - it is more widely
supported and faster. You should also test for the focus method before
trying to call it:

var el = document.forms[0].elements['ff'];
if (el.focus) el.focus();

}
window.onload = setfocus();

Your issue is here:

window.onload = setfocus; // Remove ()

[...]
 
M

Matt Kruse

I want a form field to be selected when the page loads.
any ideas what I am doing wrong?
window.onload = setfocus();

should be:

window.onload = setfocus;
 
I

ihsan.malik

solution 1 window.onload = setfocus;
solution 2
function window.onload()
{
document.getElementById("ff").focus();
}
 
R

Richard Cornford

solution 1 window.onload = setfocus;
solution 2
function window.onload()
{
document.getElementById("ff").focus();
}

Not a solution really as a property accessor in the position where an
Identifier is expected in a function declaration is a syntax error
(which is tolerated in IE but won't pass many other browsers without
complaint).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top