I want to display the hidden text field for user to enter if they had chosen particular drop box's value. here is my snippet code
var cCust = document.getElementsByName('customerType');
cCust.onchange = cType;
function cType(){
var cCust = document.getElementsByName('customerType');
var selectValue = cCust[0].options[cCust[0].selectedIndex].value;
alert(selectValue);
if (selectValue == "trd"){
document.getElementById('tradeCustDetails').style.visibility ="visible";
document.getElementById('retCustDetails').style.visibility="hidden";
}
else{
document.getElementById('tradeCustDetails').style.visibility ="hidden";
}
}
<form id= "bookingForm">
<section id="placeOrder">
<h2>Place order</h2>
Customer Type: <select name="customerType">
<option value="">Customer Type</option>
<option value="ret">Customer</option>
<option value="trd">Trade</option>
</select>
<div id="retCustDetails" class="custDetails">
Forename <input type="text" name="forename" value=""/>
Surname <input type="text" name="surname" value ="" />
</div>
<div id="tradeCustDetails" class="custDetails" style="visibility:hidden">
Company Name <input type="text" name="companyName" />
</div>
</section>
</form>
There is no error shown in console.
Here is the reference that I refer to. See here
It changes the getElementById to getElementsByNames according to my form attributes.
Is there anything I missed or mistake??