Pure Javascript - no libraries, no jquery.
function myFunction() {
var x = document.getElementById("mySelect");
var z = x.options[x.selectedIndex].value;
if (z == "outro") {
hiddenDiv1.style.display='inline-block';
}
else{
hiddenDiv1.style.display='none';
}
if (z == "maquina") {
hiddenDiv2.style.display='inline-block';
}
else{
hiddenDiv2.style.display='none';
}
}
#hiddenDiv1,#hiddenDiv2 {
animation: fadein 2s;
-moz-animation: fadein 2s; /* Firefox */
-webkit-animation: fadein 2s; /* Safari e Chrome */
-o-animation: fadein 2s; /* Opera */
}
@keyframes fadein {
from {
opacity:0;
}
to {
opacity:1;
}
}
@-moz-keyframes fadein { /* Firefox */
from {
opacity:0;
}
to {
opacity:1;
}
}
@-webkit-keyframes fadein { /* Safari e Chrome */
from {
opacity:0;
}
to {
opacity:1;
}
}
@-o-keyframes fadein { /* Opera */
from {
opacity:0;
}
to {
opacity: 1;
}
}
/* Efeito Place Holder CSS no input */
::-webkit-input-placeholder {
color: #000;
opacity: 0.5;
-webkit-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:-moz-placeholder {
color: #000;
opacity: 0.5;
-moz-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
::-moz-placeholder {
color: #000;
opacity: 0.5;
-moz-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:-ms-input-placeholder {
color: #000;
opacity: 0.5;
-ms-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
/* Place Holder CSS for Focus */
:hover::-webkit-input-placeholder {
opacity: 0.75;
-webkit-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:hover:-moz-placeholder {
opacity: 0.75;
-moz-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:hover::-moz-placeholder {
opacity: 0.75;
-moz-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:hover:-ms-input-placeholder {
opacity: 0.75;
-ms-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
/* Place Holder CSS for Focus */
:focus::-webkit-input-placeholder {
opacity: 0;
-webkit-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:focus:-moz-placeholder {
opacity: 0;
-moz-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:focus::-moz-placeholder {
opacity: 0;
-moz-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
:focus:-ms-input-placeholder {
opacity: 0;
-ms-transition: opacity 0.35s ease-in-out;
transition: opacity 0.35s ease-in-out;
}
<select id="mySelect" onchange="myFunction()">
<option value="">- Tipo Problema -</option>
<option value="maquina">Maquina</option>
<option value="vazamento">Banheiro</option>
<option value="arcondicionado">Ar Condicionado</option>
<option value="outro">Outro</option>
</select>
<div id="hiddenDiv1" style="display:none">
<input type="text" placeholder="Descreva o problema">
</div>
<div id="hiddenDiv2" style="display:none">
<select class="form-control select" name="estado">
<option value="">Selecione</option>
<option value="1">Maquina 1</option>
<option value="2">Maquina 2</option>
<option value="3">Maquina 3</option>
<option value="4">Maquina 4</option>
</select>
</div>
Discovering the selected value
The variable x
has the combobox, so we can access some properties such as selectedIndex
that contains an integer value with the index of the option
item selected.
Explaining better, each option tag has an index, in its example we have the following:
índice option
0 <option value="">- Tipo Problema -</option>
1 <option value="maquina">Maquina</option>
2 <option value="vazamento">Banheiro</option>
3 <option value="arcondicionado">Ar Condicionado</option>
4 <option value="outro">Outro</option>
When the user clicks on a combobox option, the following code will show the chosen index:
x.selectedIndex
Each option has at least 2 interesting properties: text
and value
:
1 - text
is the "label", the text between the tags <options></options>
.
x.options[x.selectedIndex].text;
2 - value
is the "key" value of the value property.
x.options[x.selectedIndex].value;
Another way
If you want to apply the effects of the above example just apply the CSS
function show(aval) {
if (aval == "outro") {
hiddenDiv1.style.display='inline-block';
}
else{
hiddenDiv1.style.display='none';
}
if (aval == "maquina") {
hiddenDiv2.style.display='inline-block';
}
else{
hiddenDiv2.style.display='none';
}
}
<select onchange="java_script_:show(this.options[this.selectedIndex].value)">
<option value="">- Tipo Problema -</option>
<option value="maquina">Maquina</option>
<option value="vazamento">Banheiro</option>
<option value="arcondicionado">Ar Condicionado</option>
<option value="outro">Outro</option>
</select>
</div>
<div id="hiddenDiv1" style="display:none">
<input type="text" placeholder="Descreva o problema">
</div>
<div id="hiddenDiv2" style="display:none">
<select class="form-control select" name="estado">
<option value="">Selecione</option>
<option value="1">Maquina 1</option>
<option value="2">Maquina 2</option>
<option value="3">Maquina 3</option>
<option value="4">Maquina 4</option>
</select>
</div>