/    Sign up×
Community /Pin to ProfileBookmark

Generic Prompt

I am trying to create a generic data prompt for my forms.

Basically, I want a prompt function that takes as arguments a form name, a field name, and a message to display on the prompt. This way I can simply call the function with some arguments in the following way:

<a href=”javascript:chpw.id.value=12345;gen_prompt(‘Enter new password’,’chpw’,’newpw’);gen_prompt(‘Confirm new password’,’chpw’,’conpw’);chpw.submit();”>Change Password</a>

Where chpw is the name of the form and newpw and conpw are the names of two hidden fields within that form. My current function looks like this:

function gen_prompt(msg,frm,fld)
{
ans = prompt(msg,””);
document.frm.fld.value=ans;
}

The problem with this is that javascript does not interpolate the variables into the document string. It tries to write the value to a literal form called frm and a literal field called fld. Is there anyway to cause the variables to be interpolated correctly? Is there another way to do this?

Thanks.
–Jason

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@AdamBrillJan 17.2003 — Try using this for your function:
<i>
</i>function gen_prompt(msg,frm,elm)
{
ans = prompt(msg,"");
eval("document."+frm+"."+elm+".value='"+ans+"'");
}

Then, call it the same way as you did before...
×

Success!

Help @jkusar 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.16,
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,
)...