This code is displaying the dropdown value in textbox on selection. But i want to display my own values in textbox instead of those default values(Default values of dropdown). how could i do that thing . I have a text box with id "#amount".
This is HTML code:
<select id="d-name" class="form-control" name="menu">
<option value="Left Chest">Left Chest</option>
<option value="Simple back">Simple back</option>
<option value="Complex back">Complex back</option>
</select>
<input type="text" id="amount" />
This is javascript code:
jQuery('#d-name').change(function(e){
var selectedValue = jQuery(this).val();
jQuery("#amount").val(selectedValue);
});
You can use some conditions -
var selectedValue = jQuery(this).val();
var amount = '$0';
if(selectedValue == 'Left Chest')
amount = '$20';
else if (selectedValue == 'Simple back')
amount = '$30';
...
jQuery("#amount").val(amount);
You can also use switch cases
.
Try this.
<select id="d-name" class="form-control" name="menu">
<option value="$20">Left Chest</option>
<option value="Simple back">Simple back</option>
<option value="Complex back">Complex back</option>
</select>
You can also change the amount by using switch case:
jQuery('#d-name').change(function(e){
var selectedValue = jQuery(this).val();
switch(selectedValue) {
case "Left Chest":
amount = "$20"; // amount which u need
break;
case "Simple back":
amount = "Simple back"; // or amount if u need
break;
case "Complex back":
amount = "Complex back"; // or amount if u need
break;
default:
amount = "";
}
jQuery("#amount").val(amount);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<select id="d-name" class="form-control" name="menu">
<option value="Left Chest">Left Chest</option>
<option value="Simple back">Simple back</option>
<option value="Complex back">Complex back</option>
</select>
<input type="text" id="amount" />
</div>
Try,
<select id="d-name" class="form-control" name="menu">
<option>Please select...</option>
<option value="$20">Left Chest</option>
<option value="Simple back">Simple back</option>
<option value="Complex back">Complex back</option>
</select>
<input type="text" id="amount" />
<script type="text/javascript">
$('#d-name').change(function(e){
var selectedValue = $(this).val();
$("#amount").val(selectedValue);
});
</script>