/    Sign up×
Community /Pin to ProfileBookmark

Hi I’m trying to make a from which has a select box and if the value is “Other” then a text box will appear (not a popup, but actually a form on the page) asking “Explain:”. Is this possible?

THX, Stale Pretzel

to post a comment
HTML

9 Comments(s)

Copy linkTweet thisAlerts:
@CharlesApr 18.2006 — <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">

<script type="text/javascript">
if (document.getElementById) onload = function () {
document.getElementById ('describe').className = 'hide'
document.getElementById ('more').onclick = function () {
document.getElementById ('describe').className = this.checked ? '' : 'hide'
}
}
</script>

<style type="text/css">
fieldset{margin:auto; width:15em}
label {display:block; margin:1ex; text-align:left}
input {margin-left:1ex}
button {display:block; margin:auto}
.hide {display:none}
</style>

</head>
<form action="some-script.pl">
<fieldset>
<legend>Foo</legend>
<label><input id="more" name="more" type="checkbox">Bar</label>
<label id="describe">More <textarea></textarea></label>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
Copy linkTweet thisAlerts:
@stalepretzel59authorApr 19.2006 — Whoa, I never really realized how much of a newbie I am. Could someone explain how this works?
Copy linkTweet thisAlerts:
@stalepretzel59authorMay 18.2006 — I don't get this, could someone help... it works but I don't know why.
Copy linkTweet thisAlerts:
@the_treeMay 18.2006 — What specifically do you not understand?

The script is twofold, it first writes the class of the relevant element (in this case a label but it could just as easily be a div or a p) and then it gives it an onclick function to the relevant select element that will change the class back, so that the css will show it again.
Copy linkTweet thisAlerts:
@stalepretzel59authorMay 22.2006 — How does this part of it work?

[CODE]<script type="text/javascript">
if (document.getElementById) onload = function () {
document.getElementById ('describe').className = 'hide'
document.getElementById ('more').onclick = function () {
document.getElementById ('describe').className = this.checked ? '' : 'hide'
}
}
</script>[/CODE]
[/QUOTE]

I'm just seriously confused...
Copy linkTweet thisAlerts:
@slaughtersMay 22.2006 — All you really need is to use a simple Hide/Show function to hide the text area "Explain" box when something besides "Other" is chosen. Using this approach on the above example gives you this:&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
function ShowIt(idName) {
document.getElementById(idName).style.display = 'block';
}
function HideIt(idName) {
document.getElementById(idName).style.display = 'none';
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="whatever.pl"&gt;
Select Something
&lt;select&gt;
&lt;option onClick="HideIt('describe');"&gt;One&lt;/option&gt;
&lt;option onClick="HideIt('describe');"&gt;Two&lt;/option&gt;
&lt;option onClick="ShowIt('describe');"&gt;Other&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;label id="describe" style="display:none"&gt;Explain:&lt;br&gt;&lt;textarea id="Explain"&gt;&lt;/textarea&gt;&lt;/label&gt;
&lt;button type="submit"&gt;Submit&lt;/button&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@CharlesMay 22.2006 — It's quite simple, really.

It uses the W3C DOM - but not all implimentations of JavaScript support that Document Object Model. So first it looks for one of the tell-tale methods, document.getElementById, something that is a sure sign that the DOM is supported.

If found then a function is created and assigned to the onload handler. This function will alter the document but it can't do that untill the document exists which is why we call it onload.

The first thing it does is change the className of the element "describe". We do this with JavaScript because not all people use JavaScript. This might seem backwards but we don't want to hide an element unless we are certain that we can un-hide it. People without JavaScript, and people with older implimentations, will always see the toggled section. It's not exactly what we want but at least the page will stay usable.

Then it creates another function and assigns it to the onclick handler of the checkbox. And this function toggles the className of the toggeld element.
Copy linkTweet thisAlerts:
@stalepretzel59authorMay 31.2006 — I still don't get this part:

document.getElementById ('describe').className = this.checked ? '' : 'hide'
Copy linkTweet thisAlerts:
@the_treeMay 31.2006 — I still don't get this part:
document.getElementById ('describe').className = this.checked ? '' : 'hide'[/QUOTE]

[b]document[/b] look in this document,
[b]getElementById('describe')[/b] find an element with the id of 'describe'.
[b]className[/b] it's class...
[b]=[/b] should be equal to,
[b]this.checked ?[/b] depending on the checked value of the element in question,
[b]'':'hide'[/b] '' if true and 'hide' if false.
×

Success!

Help @stalepretzel59 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.25,
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,
)...