I have a small problem with some things that I am doing in js and that are not working well for me.
The issue is like this: I have a select where the user chooses options:
<select id="cobertura" name="cobertura">
<option value="">Elija una opción...</option>
<option value="1">Opción 1</option>
<option value="2">Opción 2</option>
<option value="3">Opción 3</option>
</select>
Depending on what the user selects, it will show me or not certain "divs" on the page for what I do:
<div id="div_1" class="contenido">
<!-- muestro contenido -->
</div>
<div id="div_2" class="contenido">
<!-- muestro otro contenido diferente -->
</div>
Now, when the user chooses option 3, what I have to show are the two divs for this I do:
<script type="text/javascript">
$(document).ready(function(){
$(".contenido").hide();
$("#cobertura").change(function(){
$(".contenido").hide();
$("#div_" + $(this).val()).show();
});
});
</script>
Now, this validation before the select event works fine, that is, it shows me only div1 in option 1 and only div2 in option 2, but I don't know how to build it to show me the two divs with option 3
Can someone guide me?
Without complicating things, what you can do is save in the
option
as an attribute the selector of the element that shows and so when the selected option changes, you get the value of the attribute and show it:What this does is that as option 1 only shows the
#div_1
then I assign the selector of which it is going to show and in the case of 3, then he added the selectors of the ones that it shows that are#div_1
and#div_2
.Taking into account your structure and that it will always be that logic, I would make a condition that asks when the value is equal to 3, and if it is true then it shows the two content containers:
I leave you the functional example:
what you can do is compare if the value of the Select is equal to 3 show all the elements through its class something like this: