Connecting Tech Pros Worldwide Forums | Help | Site Map

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

linuxnooby@yahoo.com.au
Guest
 
Posts: n/a
#1: Nov 27 '06
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>




RobG
Guest
 
Posts: n/a
#2: Nov 27 '06

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



linuxnooby@yahoo.com.au wrote:
Quote:
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).
Quote:
>
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();

Quote:
}
window.onload = setfocus();
Your issue is here:

window.onload = setfocus; // Remove ()

[...]

--
Rob

linuxnooby@yahoo.com.au
Guest
 
Posts: n/a
#3: Nov 27 '06

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


Your issue is here:
Quote:
>
window.onload = setfocus; // Remove ()

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

Matt Kruse
Guest
 
Posts: n/a
#4: Nov 27 '06

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


linuxnooby@yahoo.com.au wrote:
Quote:
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


ihsan.malik@gmail.com
Guest
 
Posts: n/a
#5: Nov 27 '06

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


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


Matt Kruse wrote:
Quote:
linuxnooby@yahoo.com.au wrote:
Quote:
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
Richard Cornford
Guest
 
Posts: n/a
#6: Nov 27 '06

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


ihsan.malik@gmail.com wrote:
Quote:
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.

mridley1@gmail.com
Guest
 
Posts: n/a
#7: Dec 5 '06

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


Awsome! This thread helped me! Thanks guys!

Closed Thread


Similar JavaScript / Ajax / DHTML bytes