/    Sign up×
Community /Pin to ProfileBookmark

Setting Focus to a variable

Hi I’m trying to dynamically set focus to a form element. Here is what I have.

function isDate(val,format,formField) {
var date=getDateFromFormat(val,format);
if (date==0) {

alert(“this is ” + formField +” has a wrong date”);
//document.CurrentForm.”formField”.focus();
//document.CurrentForm.ItemAddedOnDate.focus();
document.CurrentForm.focus(‘formField’);

return false;
}
else {
alert(“this is a right date”);

return true;
}
}

I’ve tried test it a few ways I know my variable “formField” has the right value because I have added it into the alert and it is displayed fine and I know that my focus works when I hard code the form field name “ItemAddedOnDate”, however I can’t for the life of me get it to work if I put the variable formfield in the focus. Does anyone have anyidea what I’m doing wrong or how I can get this to work.

TIA

Nikki

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@requestcodeDec 17.2003 — Try this:

document.CurrentForm.elements[formField].focus();
Copy linkTweet thisAlerts:
@nbenton99authorDec 17.2003 — Thank you

I feel much better now.

That works great!!!
×

Success!

Help @nbenton99 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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