Click here to Skip to main content
16,017,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
fill the textbox on change of a dropdown value selected using javascript
Posted
Updated 28-Mar-14 1:00am
v4

HTML
<select onchange="jsFunction()">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

JavaScript
function jsFunction(){
   // set text box value here
   var txt =  document.getElementById('txtBox');
   txt.value = "assign_here";
}



[Edit member="Tadit"]
Added pre tags.
[/Edit]
 
Share this answer
 
v3
you need to write Html code similar as:
<select Id="myDropdown">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>

<input id="txtBox" type="text">

jQuery Code as:
$("#myDropdown").change(function () {
    var selectedValue = $(this).val();
    $("#txtBox").val($(this).find("option:selected").attr("value"))
});



[Edit member="Tadit"]
Removed extra pre tags.
Formatted texts.
[/Edit]
 
Share this answer
 
v2
you can try this

C#
function chkind(){
   var dropdown1 = document.getElementById('dropdown1');
   var textbox = document.getElementById('textbox');
   if(dropdown1.selectedIndex == 0){
     textbox.value = "hi";
   } else if(dropdown1.selectedIndex == 1) {
     textbox.value = "bye";
   }
   }



try this also to get index value
var a = dropdown1.options[dropdown1.selectedIndex].value;

like this
XML
function chkind(){
   var dropdown1 = document.getElementById('dropdown1');
   var a = dropdown1.options[dropdown1.selectedIndex].value;
   var textbox = document.getElementById('textbox');
     textbox.value = a;  
   }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900