aot2002
asked on
how to make select dropdown with HIDE SHOW CSS
<select name=changeme onchange="this.style=displ ay:block; everythingelse.style=displ ay:none">
<DIV ID="test" style="display:block">
</DIV>
<DIV ID="test2" style="display:none">
</DIV>
<DIV ID="test3" style="display:none">
</DIV>
<DIV ID="test" style="display:block">
</DIV>
<DIV ID="test2" style="display:none">
</DIV>
<DIV ID="test3" style="display:none">
</DIV>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yup this small addition does identify only "test" divisions.
<script type="text/javascript"><!- -
function display(theId) {
var e = document.getElementsByTagN ame("div") ;
for (var i=0; i<e.length; i++)
if (/^test/i.test(e[i].id))
e[i].style.display = "none";
document.getElementById(th eId).style .display = "block"; // Show the element
}
//--></script>
<div id="header"><h1>Head</h1>< /div>
<select onchange="display(this.val ue);">
<option value="test">Test</option>
<option value="test2">Test2</optio n>
<option value="test3">Test3</optio n>
<option value="test4">Test3</optio n>
<option value="test5">Test3</optio n>
</select>
<div id="test" style="display:none;">test </div>
<div id="test2" style="display:none;">test 2</div>
<div id="test3" style="display:none;">test 3</div>
<div id="test4" style="display:none;">test 4</div>
<div id="test5" style="display:none;">test 5</div>
Notice now that the <div id="header"> is never hidden even though it is in the e[] array because it's tag name is <div>.
Thanks for the lesson sam2912. Helped me learn some really dynamic code! So thanks =-)
Joe P
<script type="text/javascript"><!-
function display(theId) {
var e = document.getElementsByTagN
for (var i=0; i<e.length; i++)
if (/^test/i.test(e[i].id))
e[i].style.display = "none";
document.getElementById(th
}
//--></script>
<div id="header"><h1>Head</h1><
<select onchange="display(this.val
<option value="test">Test</option>
<option value="test2">Test2</optio
<option value="test3">Test3</optio
<option value="test4">Test3</optio
<option value="test5">Test3</optio
</select>
<div id="test" style="display:none;">test
<div id="test2" style="display:none;">test
<div id="test3" style="display:none;">test
<div id="test4" style="display:none;">test
<div id="test5" style="display:none;">test
Notice now that the <div id="header"> is never hidden even though it is in the e[] array because it's tag name is <div>.
Thanks for the lesson sam2912. Helped me learn some really dynamic code! So thanks =-)
Joe P
I would have done that regex thing, but didn't want to confuse the original question.
Excellent. I'm learning along with aot2002. =)
Thanks,
Joe P
Thanks,
Joe P
That way the javascript for loop would be:
for (var i=1; i<6; i++)
document.getElementById("t
This specifies just those 5 "test" divisions get hidden before one gets shown. If you had other divisions on your page, say <div id="header">, <div id="footer">, <div id="nav">, then your code would need a slight change because your's uses getElementsByTagName("div"
The advantage that your's has over mine is that your's does not need to know the specific amount of divisions. Mine has hardcoded (although you can work around this). Yours includes all the divisions and so no hard coded number is given.
I am not too familer with DOM code, (I get by with hacking up scripts and learning as I go). What would be your solution to test to see if the division that you have has an id with test. Would your code be like this?:
var e = document.getElementsByTagN
var regex = /^test/i;
for (var i=0; i<e.length; i++)
{
if (regex.test(e[i].id))
e[i].style.display = "none";
}
I think that looks right, it checks the if the id starts with the word "test", if so then it hides that division.
What do you think?
Joe P
Joe P