/    Sign up×
Community /Pin to ProfileBookmark

focus problem

Hi

I want to set the focus on the field that must be filled
i am using this script:
<?php
print “<script>
print “document.myform.$fieldname.focus();”;
print “</script>”;
?>

but i am getting the error:
document.myform.fieldname is null or is not an object.

How can i fix?
there is another javascript sintax i can use to focus on an object?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@fredmvApr 19.2004 — What's going on is that you're executing that script before the rest of the document exists thus the form nor child elements of it exist. You must run the script when everything is parsed, loaded, and initalized using the [font=courier]onload[/font] event handler.&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
onload = function()
{
// ...
}
//]]&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@amrigoauthorApr 19.2004 — Great!

it can be writed in the head section or any section?

how it will be ? just like you wrote:

<script>

onLoad = Function get_focus(){

document.myform.myfield.focus();

}

</script>
Copy linkTweet thisAlerts:
@fredmvApr 19.2004 — The [font=courier]<head>[/font] section would be preferable, but it is completely valid to have it within [font=courier]<body>[/font] as well. Moreover, you can just write it like I had it &#8212; as an annoymous function (i.e., a function with no name).
Copy linkTweet thisAlerts:
@amrigoauthorApr 19.2004 — thank´s a lot i was wondering if a return true or false is needed a t the end of the function ??
Copy linkTweet thisAlerts:
@fredmvApr 19.2004 — No problem and, no, you wouldn't need any kind of return value in this case.
Copy linkTweet thisAlerts:
@amrigoauthorApr 19.2004 — thank´s al ot you save me a lot of time
×

Success!

Help @amrigo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...