I need to show/hide some divs depending on selected options. I’m using .change function.
HTML looks more-less like this:
<div id="1" style="display:none">
</div>
<div id="WRAP">
<div id="2" style="display:none">
</div>
<div id="3" style="display:none">
</div>
</div>
<div id="4" style="display:none">
</div>
jQuery:
$(function(){
$('#fyo').change(function(){
$('#' + $(this).val()).show('slow').children().show('slow');
$('#' + $(this).val()).siblings().hide('slow');
$('#' + $(this).val()).parent().show('slow').siblings().hide('slow');
});
});
I won’t write the code for options but just FYI the options are as follows:
Choose option: 1 or 2 or 3 or 4 or WRAP (so 2 & 3).
It should be possible to:
- show each of these divs separately, including inner divs – with all other divs hidden;
<div id="WRAP">should show all inner divs simultaneously.
The result I achieved is (almost) exactly what I needed but as I’m a beginner with jQuery, I was wondering if this is correct? Or is there any easier way to achieve this? There is one thing that makes me doubt that something is actually wrong – when I choose 2 or 3, it doesn’t display in the style of 'slow', but instead it’s almost immediate and looks like something is wrong because it doesn’t come out so smoothly.
this will potentially mess with element you didnt mean to change. For instance if
and you select 1 then
its best to give them all a similar class like “hideableDiv” and do manipulations using that as a selector;