/    Sign up×
Community /Pin to ProfileBookmark

drop down sets input field? how do I do it?

I have a form on site that asks if you want to be contacted via phone. You can select ‘Yes” or ‘No’ via a drop down. The field after the drop down is an input field that asks for the phone number if you answered ‘Yes” to the above question. Well, I think it would be better that if you answered “Yes” to the drop down question that the phone number input filed either (1) becomes active and you can type in it OR (2) has a text message show up in it that says “be sure to provide your phone number”. If “No” is selected, you cannot enter or use the phone number filed would be optional. Point is, I am looking for a way to prompt people to enter the number in the filed if they answer “Yes” and I think a drop down that sets an input field would be a neat solution.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@pyroSep 24.2003 — Try something like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function setField(frm) {
if (frm.yesno.options[frm.yesno.selectedIndex].value == "yes") {
frm.phonenum.value = "Please input your phone number";
}
else {
frm.phonenum.value = "You my optionally enter your phone number";
}
}
</script>
</head>
<body>
<form name="myform" action="">
<p><select name="yesno" onchange="setField(this.form);">
<option value="">Please Choose</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<input type="text" name="phonenum"></p>
</form>
</body>
</html>
Copy linkTweet thisAlerts:
@etardauthorSep 25.2003 — peeeeeerfect. exactly what I wanted. I added an onFocus element to the phone field to clear it when the cursor is put into so they can add their phone number and BAM- works perfect. Thanks!
Copy linkTweet thisAlerts:
@pyroSep 25.2003 — You're welcome... ?
×

Success!

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

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

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