Reputation: 185
I have a select list in html and I want to get the select values in a textbox. For this I have done the following code:
<script type="text/javascript">
$(document).ready(function(){
$("#myDropdown").change(function () {
var selectedValue = $(this).val();
$("#txtBox").val($(this).find("option:selected").attr("value"))
});
});
</script>
<table id="tab1">
<tr>
<td>Select Affiliate Source:</td>
<td>
<select id="myDropdown">
<option value="vcom">Vcommission</option>
<option value="pym">Payoom</option>
<option value="snpdl">Snapdeal</option>
<option value="flpkrt">Flipkart</option>
</select>
<div><input id="txtBox" type="text"></div>
</td>
</tr>
</table>
But while selecting the value in select the textbox is not showing any value.
Upvotes: 1
Views: 1060
Reputation: 6031
If you use the code below, it should provide the desired result.
To retrieve the value of a select
element in jQuery, you only need to call the val()
function of the element as shown below.
$(document).ready(function(){
$("#myDropdown").change(function () {
var selectedValue = $(this).val();
$("#txtBox").val(selectedValue);
});
})
Source: http://api.jquery.com/val/
Upvotes: 0
Reputation: 15933
$(document).ready()
selectedValue
, no need to use find()
$(document).ready(function() {
$("#myDropdown").change(function() {
var selectedValue = $(this).val();
$("#txtBox").val(selectedValue);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
<table id="tab1">
<tr>
<td>Select Affiliate Source:</td>
<td>
<select id="myDropdown">
<option value="vcom">Vcommission</option>
<option value="pym">Payoom</option>
<option value="snpdl">Snapdeal</option>
<option value="flpkrt">Flipkart</option>
</select>
<div>
<input id="txtBox" type="text">
</div>
</td>
</tr>
</table>
Upvotes: 1
Reputation: 5222
Script should be execute after the full page load or you can move the script at the end of the page. Try this,
$(document).ready(function(){
$("#myDropdown").change(function () {
var selectedValue = $(this).val();
$("#txtBox").val(selectedValue);
});
})
Upvotes: 0
Reputation: 40639
Use $.ready(), as your DOM is loaded after your jquery code, so that your element event not applied,
$(function(){
$("#myDropdown").change(function () {
$("#txtBox").val(this.value); // sometimes core javascript functionality more easy to use
});
});
Upvotes: 0
Reputation: 1075427
You already have the value in selectedValue
, no need for the find
:
$("#myDropdown").change(function () {
var selectedValue = $(this).val();
$("#txtBox").val(selectedValue);
});
Separately, the code as shown in your question will try to access the select
element before it exists, and won't attach a change
handler to it. I don't know if that's really how your code is arranged, but if so, that's a problem as well. Script code only has access to elements defined before it runs. You need to either move that script
tag below the select
tag (usually just before the ending </body>
tag is best), or use jQuery's ready
callback (but only if you don't control where the script tags go).
Here's a complete example, in order:
<table id="tab1">
<tr>
<td>Select Affiliate Source:</td>
<td>
<select id="myDropdown">
<option value="vcom">Vcommission</option>
<option value="pym">Payoom</option>
<option value="snpdl">Snapdeal</option>
<option value="flpkrt">Flipkart</option>
</select>
<div><input id="txtBox" type="text"></div>
</td>
</tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$("#myDropdown").change(function () {
var selectedValue = $(this).val();
$("#txtBox").val(selectedValue);
});
</script>
Upvotes: 1