469,950 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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

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>

Nov 27 '06 #1
6 4428

li********@yahoo.com.au wrote:
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 ()

[...]

--
Rob

Nov 27 '06 #2
Your issue is here:
>
window.onload = setfocus; // Remove ()

thanks for the answer, my brain was starting to melt
Dave

Nov 27 '06 #3
li********@yahoo.com.au wrote:
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;

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 27 '06 #4
solution 1 window.onload = setfocus;
solution 2
function window.onload()
{
document.getElementById("ff").focus();
}
Matt Kruse wrote:
li********@yahoo.com.au wrote:
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;

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Nov 27 '06 #5
ih*********@gmail.com wrote:
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.

Nov 27 '06 #6
Awsome! This thread helped me! Thanks guys!

Dec 5 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Aaron | last post: by
7 posts views Thread by dennis.sprengers | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.