Kirataka Kirataka - 16 days ago 8
HTML Question

How to display a div when values are selected from TWO dropdown?

Here I have two dropdown

<div class="form-group">
<label class="col-sm-4 control-label">Car Type</label>
<div class="col-sm-7">
<select class="form-control" id="cartype" name="cartype">
<option value=""> -- Select Type -- </option>
<option value="Innova">Innova</option>
<option value="Tata">Tata</option>
<option value="Mahindra">Mahindra</option>
</select>
</div>
</div>

<div class="form-group">
<label class="col-sm-4 control-label">Rate Type</label>
<div class="col-sm-7">
<select class="form-control" id="rate" name="rate" onchange="showdiv()" >
<option value=""> -- Select Type -- </option>
<option value="Outstation" name="outstation" id="outstation">Outstation</option>
<option value="Local" name="local" id="local">Local</option>
</select>
</div>




And a Text Box

<div class="form-group" id="showme" style="display:none;">
<input type="text" id="txt1" name="txt1">
</div>


My Javascript

function showdiv(){
var vehicle = document.getElementById("cartype").value = "Innova || Tata";
var rate = document.getElementById("rate").value = "Local";
document.getElementById("showme").style.display = 'block';
}


Here I need to display the Text Box
div
only when cartype is equal to
Local
. But when I select
Rate Type
the hidden
div
is displaying.

Can you help me out where is the error in my code??

Answer

this should be working:

function showdiv(){
  var vehicle = document.getElementById("cartype").value;
  var rate = document.getElementById("rate").value;

  if ((vehicle == "Innova" || vehicle == "Tata") && rate == 'Local') {
     document.getElementById("showme").style.display = 'block';
  } else {
     document.getElementById("showme").style.display = 'none';
  }
}
Comments