PeakGen PeakGen - 3 months ago 10
Javascript Question

Unable to compare values in dropdown box and textfield in JavaScript

Please have a look at my below code

javascript

<script>
$(document).ready(function () { // Help for the HTML4 version.
$('select[name=brandTxtSelect]').change(function () {

if($(this).val()=="Select")
{
document.getElementById('brandTxt').readOnly = false;
document.getElementById("brandTxt").style.display = "block";
}
else
{
document.getElementById('brandTxt').readOnly = true;
document.getElementById("brandTxt").style.display = "none";
$('input[name=brandTxt]').val($(this).val());
}

});
});
</script>


HTML

<div class="form-group">
<label class="col-md-4 control-label" for="brandTxt">Brand</label>
<div class="col-md-4">
<select name="brandTxtSelect" class="form-control input-md">
<option value="">Select</option>
<option value="A">A</option>
<option value="B">B</option>
</select>

<!-- <input id="textinput" name="brandTxt" type="text" placeholder="Type Drug Brand" class="form-control input-md" > -->
</div>
<div class="col-md-4">
<input type="text" id="brandTxt" name="brandTxt" class="form-control input-md">
</div>

</div>


I am trying to make the textfield visible and enabled when "Select" is selected from the dropdown box. Unfortunately it seems always the
else
part is getting fired. I must be doing something wrong in JavaScript, please advice.

Answer

Your value for "Select" is not select, is "".

Change:

<option value="">Select</option>

for:

<option value="Select">Select</option>
Comments