Hide and Show DIV based on SELECT response

Asked

Viewed 2,357 times

1

I have the following code and would like when selecting any value within select "#Cities" to show the div ". dataLoja" and that it is hidden at first, already taking advantage of the question has something else that I am not getting that is to put the value "Select" in select "#Cities", as value or with placeholder, none worked (ps:the information from select comes from another JS).

     <div class="row">
        <div class="col-lg-12 text-center">
            <span class="TituloLoja">Selecione sua localidade: </span>
            <select id="cities" class="empty" name="cities"></select>
        </div>
    </div>

   <div class="row content" >
         <div class="well text-left">
            <div class="dadosLoja ">
                <span class="TituloLoja">Informações da Loja:</span><br />
                <label>Telefone: <span id="telefone"></span></label>
            </div>
        </div>
    </div>

1 answer

4


Explanation:

To leave the div .dadosLoja hidden, I put her as display:none;. When an option is selected and this is different from "Select" the javascript changes the display of the div .dadosLoja for display:block;. The "Select" option was included directly in html with the property selected, so by default she is already selected.

Upshot:

function selected(value){
var dadosLoja = document.getElementsByClassName('dadosLoja');
	if(value != "Selecionar"){
    dadosLoja[0].style.display = 'block';
  }else{
  dadosLoja[0].style.display = 'none';
  }
}
     <div class="row">
        <div class="col-lg-12 text-center">
            <span class="TituloLoja">Selecione sua localidade: </span>
            <select onclick="selected(this.value)" id="cities" class="empty" name="cities">
              <option selected>Selecionar</option>
              <option>Teste 2</option>
              <option>Teste 3</option>
              <option>Teste 4</option>
            </select>
        </div>
    </div>

   <div class="row content" >
         <div class="well text-left">
            <div class="dadosLoja" style="display:none;">
                <span class="TituloLoja">Informações da Loja:</span><br />
                <label>Telefone: <span id="telefone"></span></label>
            </div>
        </div>
    </div>

You can see the code here also.

  • Can you please post your Jsfiddle content in the reply and leave the link only for reference? If the link goes offline the Sopt community can continue to consult this question.

  • Okay, thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.