/    Sign up×
Community /Pin to ProfileBookmark

radio button focus not working, kind of.

hi trying to get a bot check radio button to focus on a submit check, if not selected. why would this not work in a function but work fine running on it’s own during the script?

so it does not work like this:

`function checkf() {

if (!document.getElementById(“names”).value) {
alert(‘Please enter a value for name’);
document.getElementById(“names”).focus();
var error=1;
} else if (!document.getElementById(“tel”).value) {
alert(‘Please enter a value for contact number’);
document.getElementById(“tel”).focus();
var error=1;
} else if (!document.getElementById(“emails”).value) {
alert(‘Please enter a value for email’);
document.getElementById(“emails”).focus();
var error=1;
} else if (!document.getElementById(‘BotCheck’).checked) {
alert(‘Please Click Bot Check’);
**document.getElementById(‘BotCheck’).focus();**
var error=1;
}
if (!error) document.book.submit();

}`

but iot work fine like this:

`<script type=”text/javascript”>
document.getElementById(‘BotCheck’).focus();
</script>`

_weird.._

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 27.2020 — I fiddled some time and didn't get it working. Seems to have something to do with the alert: When I replace it by console.log the focussing works fine. However this is no solution as the message is not visible to the user then.

I recomment to switch to the built in form validation: Simply add an attribute "required" to any of your form elements and the browser will do the rest for your.
×

Success!

Help @hitecbill 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 4.30,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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