View Full Version : javascript question
error_22
05-11-2006, 05:26 PM
Hi,
Im trying to make a form....but I think I need a javascript to get it the way i want it.
I have this html code:
<select name="ltype">
<option selected>Choose type</option>
<option value="maindir">Main menu - direct link</option>
<option value="subdir">Sub menu - direct link</option>
</select>
If the first option is selected (Main menu - direct link) i want my next part of the form to show up. If the second option is selected i want another part of the form to show up. Is this possible with javascript? Where can i find information about this?
Thanks in advance!
Chroder
05-13-2006, 06:21 PM
You need to use JS to toggle the 'display' CSS property of the form parts. When a page element has its display set to 'none', then it is removed from view. So you need to attach an 'onchange' event to the select box, and show/hide parts of the form depending on what is selected.
Here's an example:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function showpartChanged(obj)
{
// Go through the options to hide/show
// each section
for(i = 0; i < obj.options.length; i++)
{
// Ge the value (ie: part1)
val = obj.options[i].value;
// Create the id of the form parts (ie: form_part1)
form_part = 'form_' + val;
// If its selected, then show the form part
if(obj.options[i].selected)
document.getElementById(form_part).style.display = '';
// It should be hidden
else
document.getElementById(form_part).style.display = 'none';
}
}
</script>
</head>
<body>
<select id="showpart" onchange="showpartChanged(this)">
<option value="part1" selected="selected">Part 1</option>
<option value="part2">Part 2</option>
<option value="part3">Part 3</option>
</select>
<div id="form_part1">
Form Part 1
</div>
<div id="form_part2" style="display:none;">
Form Part 2
</div>
<div id="form_part3" style="display:none;">
Form Part 3
</div>
</body>
</html>
error_22
05-15-2006, 03:23 PM
By default, I dont want any of the options to show up, how can i achieve that?
Thank you so much for taking the time to help me!
Chroder
05-15-2006, 03:24 PM
Just stick style="display:none" in the tag to hide it.
error_22
05-15-2006, 06:49 PM
great, thanks!
just one more thing....i want to add <option selected>something here</option>
but when i do that (and yes i have removed selected="selected" from form_part1) everything stops working. I mean, the parts wont show up.
Thanks in advance!
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.