I'm creating a form for an assignment. How am I able to make a new question appear upon the selection on a value on a different question? For example,
<label for="Etype">*What would you like to enrol in?</label>
<select id="Etype" name="Etype">
<option value="">Select...</option>
<option value="Camp">Camp</option>
<option value="Class">Class</option>
</select>
</fieldset>
and upon the selection of "class" a previously hidden question is revealed beneath the previous question asking "type of class?"
Put this just before your body closing tag:
<script>
document.getElementById('Etype').onchange = function() {
var isSelected = this.options[this.selectedIndex].value == 'Class';
document.getElementById('hiddenField').style.display = isSelected ? 'block':'none';
};
</script>
Assuming the hidden element has an ID 'hiddenField':
<div id="hiddenField" style="display:none;">
Place the hidden field, labels, etc. here
</div>
<fieldset>
<label for="Etype">*What would you like to enrol in?</label>
<select id="Etype" onchange="show_hidden(this.value)" name="Etype">
<option value="">Select...</option>
<option value="Camp">Camp</option>
<option value="Class">Class</option>
</select>
<div id="class">
Type of class : <input type="text" name="class_text" />
</div><!-- #class -->
<div id="camp">
Type of Camp <input type="text" name="camp_text" />
</div><!-- #camp -->
</fieldset>
#class, #camp{display:none;}
function show_hidden(str)
{
// if first option was selected, hide both the hidden fields
if(str == '')
{
document.getElementById('class').style.display = 'none';
document.getElementById('camp').style.display = 'none';
}
// if class was selected, show the class fields and hide camp fields if visible
else if(str == 'Class')
{
document.getElementById('class').style.display = 'block';
document.getElementById('camp').style.display = 'none';
}
// if camp was selected, show the camp fields and hide class fields if visible
else if(str == 'Camp')
{
document.getElementById('camp').style.display = 'block';
document.getElementById('class').style.display = 'none';
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.