/    Sign up×
Community /Pin to ProfileBookmark

Is it possible to set the focus to an object within a javascript function?

I know objectname.focus(); works, but it doesn’t work if it’s in a function.

i.e.

[code]
function testme()
{
btn.focus();
}
[/code]

How do I do this?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterJan 31.2005 — Yes it does,

whatever your trying to do the problem is elsewhere.

Possibly your problem relates to where you defined your function.

Possibly it might be where you have your function call.

Make sure to put the function inside your head.

Also, when the pages loads, it needs to load all the objects.

If your function call is too early, the object will not exist yet.

Make sure whatever code your executing is put into a function that is

executed onload.
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
function ONLD(){
mytextbox.focus()
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="ONLD()"&gt;
&lt;input type="text" ID="mytextbox"&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@JPnycJan 31.2005 — If you post the relevant HTML of whatever your trying to give focus to, we can tell you how to reference it.
Copy linkTweet thisAlerts:
@UltimaterJan 31.2005 — I do not reckon that [b]that[/b] will be necessary.

His function call is being executed, if at all, too early

before the object exists.
Copy linkTweet thisAlerts:
@JPnycJan 31.2005 — It's impossible to tell that from what he posted. We do not even know that the names are correct, or what they are. Is there a form? Are there frames involved? When's it being called? We don't know when the script executes, in fact we know next to nothing about the page at all.
Copy linkTweet thisAlerts:
@UltimaterJan 31.2005 — [i]Originally posted by JPnyc [/i]

[B]It's impossible to tell that from what he posted. We do not even know that the names are correct, or what they are. Is there a form? Are there frames involved? When's it being called? We don't know when the script executes, in fact we know next to nothing about the page at all. [/B][/QUOTE]


He was talking in general and I answered in general.

But I must admit, more information would clarify [b]exactly[/b] whats going on in his code.
×

Success!

Help @MdWebtress 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.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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