I want to populate a text box with pre-defined values. This text box is read-only. User can select from 2 inter-dependent dropdown boxes. All this is part of a form in a bootstrap project.
Taking cue from this, I am able to populate second dropdown by selecting first. However, cannot combine the functionality of both and link them to the text box.
Code:
<select id="cat">
<option val="Laptops">Laptops</option>
<option val="Phones">Phones</option>
<option val="Tablets">Tablets</option>
<option val="PC">PC</option>
</select>
<select id="item"></select>
<input id='price' type='text' readonly/>
javascript:
Laptops=new Array("HP","Dell","Apple","Lenovo");
Phones=new Array('Samsung','Nokia','iPhone');
Tablets=new Array('Apple','Lenovo','Asus','Acer','HP');
PC=new Array('IBM','Macintosh','Dell');
populateSelect();
$(function() {
$('#cat').change(function(){
populateSelect();
});
});
function populateSelect(){
cat=$('#cat').val();
$('#item').html('');
eval(cat).forEach(function(t) {
$('#item').append('<option>'+t+'</option>');
});
}
I have about 5 and 4 values in dropdowns 1 and 2 respectively. Not using database as of now.
How can we add price in text box when user selects from any of the dropdown?