/    Sign up×
Community /Pin to ProfileBookmark

Need help making an input box appear when user types text in another input box?

Hi everyone.

I’m building a form page, and one of the input boxes is optional. If the user enters text into it, I want another input box to appear for more data to be entered. Basically, I just need help setting up the Javascript to get this done.

I was planning on having the input box that’s going to appear be set to display:none with CSS, and then use a Javascript method change the DOM to make it display:block once the user enters text in the optional field. But the problem I’m having is I’m not sure how to tell when the user has entered text there. I understand there’s an onClick method for most elements, but I don’t know of anything like onTextEntered. Can someone help me with how I determine when text has been entered into that box?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeJan 14.2012 — You can use onFocus, onBlur, onKeyUp.

If you want the second field to appear when focus is put on the optional input:

[CODE]
<input type="text" name="field1" id="field1" onFocus="document.getElementById('field2').style.display = 'block';" />
<input type="text" name="field2" id="field2" />
[/CODE]


If you want the second field to appear when the optional field is blurred but only if it has a value:

[CODE]
<input type="text" name="field1" id="field1" onBlur="if(this.value != '') {document.getElementById('field2').style.display = 'block';}" />
<input type="text" name="field2" id="field2" />
[/CODE]


If you want the second field to appear when a key is pressed and enters a value:

[CODE]
<input type="text" name="field1" id="field1" onKeyUp="if(this.value != '') {document.getElementById('field2').style.display = 'block';}" />
<input type="text" name="field2" id="field2" />
[/CODE]

Many more options, but you get the point.
Copy linkTweet thisAlerts:
@RiothamusauthorJan 15.2012 — Thanks. I used the onKeyUp one to make it appear when a value is entered. But then I tried to add code to make the field disappear again if the user backspaces everything:

<i>
</i>else {
document.getElementById('field2').style.display = 'none';
}


But it doesn't work. Can you tell me what I'm doing wrong?
Copy linkTweet thisAlerts:
@RiothamusauthorJan 15.2012 — Never mind, fixed it. Thanks.

Can anyone tell me how to translate this into jQuery, by any chance?
Copy linkTweet thisAlerts:
@007JulienJan 15.2012 — See too this page and the others about forms from Peter Paul Kock.
×

Success!

Help @Riothamus 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.28,
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,
)...