Click here to Skip to main content
14,177,621 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
fill the textbox on change of a dropdown value selected using javascript
Posted
Updated 28-Mar-14 1:00am
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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



[Edit member="Tadit"]
Added pre tags.
[/Edit]
   
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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]
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

you can try this

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
function chkind(){
   var dropdown1 = document.getElementById('dropdown1');
   var a = dropdown1.options[dropdown1.selectedIndex].value;
   var textbox = document.getElementById('textbox');
     textbox.value = a;  
   }
   
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190526.1 | Last Updated 17 Oct 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100