I am trying to display the selected value from the drop down list in a text field in a wordpress post. But the code its not fetching the values.
<select name="cmbitems" id="cmbitems"><style type="text/javascript">
var select = document.getElementById('cmbitems');
var input = document.getElementById('txtprice');
select.onchange = function() {
input.value = select.value;
}
</script>
<option value="" selected="selected">Select Country</option>
<option value="£0.0">Ireland</option>
<option value="£2.50">United States</option>
<option value="£2.50">United Kingdom</option>
<option value="£2.50">Afghanistan</option>
<option value="£2.50">Albania</option>
<option value="£2.50">Algeria</option>
<option value="£2.50">American Samoa</option>
<option value="£2.50">Andorra</option>
Postage Fee:
<input type="text" name="txtprice" id="txtprice" onClick="checkPrice()">
Your html syntax is not valid put script
tag outside select
tag, inside select tag there should be only option
tags.
<script>
var select = document.getElementById('cmbitems');
var input = document.getElementById('txtprice');
select.onchange = function() {
input.value = select.value;
}
</script>
<select name="cmbitems" id="cmbitems">
<option value="" selected="selected">Select Country</option>
<option value="£0.0">Ireland</option>
<option value="£2.50">United States</option>
<option value="£2.50">United Kingdom</option>
<option value="£2.50">Afghanistan</option>
<option value="£2.50">Albania</option>
<option value="£2.50">Algeria</option>
<option value="£2.50">American Samoa</option>
<option value="£2.50">Andorra</option>
</select>
<input id="txtprice" />
Whenever your drop down value changes it will change the value in input field value. You can go through the following code :
<script>
function getValue(value) {
alert(value);
$('#txtprice').val(value);
}
</script>
<select name="cmbitems" id="cmbitems" onchange="getValue(this.value)">
<option value="" selected="selected">Select Country</option>
<option value="£0.0">Ireland</option>
<option value="£2.50">United States</option>
<option value="£2.50">United Kingdom</option>
<option value="£2.50">Afghanistan</option>
<option value="£2.50">Albania</option>
<option value="£2.50">Algeria</option>
<option value="£2.50">American Samoa</option>
<option value="£2.50">Andorra</option>
</select>
<input id="txtprice" />
You have a mistake on script start in first line:
<select name="cmbitems" id="cmbitems"> <**style** type="text/javascript">
TRy to use the correct script tag instead of
<**style** type="text/javascript">
<**script** type="text/javascript">