0

I'm here to ask a question regarding JavaScript. I'm new to JavaScript so please forgive if I'm going wrong. Could someone help me to get the values as result. After I select any State from Drop down list, the Show button should give me the value of the States as result. Is it possible?

  <h1>Please select a State</h1>

  <select id = "district">
   <option value = "Tiruvanandapuram">Kerala</option>
   <option value = "Chennai">Tamil Nadu</option>
   <option value = "New Delhi">Delhi</option>
  </select>
  
  <input type = "button" value = "Show" onclick = "" />
  <INPUT type="text" ID="add"  id="txtresult" NAME="result" VALUE="">
    

`

Shamshid
  • 403
  • 3
  • 11
  • you can find your answer here: http://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript – Asad Palekar Jan 27 '16 at 11:28

4 Answers4

1

This will do it:

var sel = document.getElementById("district");

function show(){
  var txt = document.getElementById("add");
  txt.value = sel.options[sel.selectedIndex].value;
}
<h1>Please select a State</h1>

  <select id = "district">
   <option value = "Kerala">Kerala</option>
   <option value = "Chennai">Tamil Nadu</option>
   <option value = "New Delhi">Delhi</option>
  </select>
  
  <input type = "button" value = "Show" onclick = "show()" />
  <INPUT type="text" id="add"  id="txtresult" NAME="result" VALUE="">
P. Jairaj
  • 1,033
  • 1
  • 6
  • 8
1

Try a simple code hope help

    <script type="text/javascript">
function run() {
    document.getElementById("srt").value = document.getElementById("district").value;
}
</script>

<h1>Please select a State</h1>

<select id = "district">
   <option value="Tiruvanandapuram">Kerala</option>
   <option value="Chennai">Tamil Nadu</option>
   <option value="Delhi">Delhi</option>
</select>

<input type = "button" value = "Show" onclick="run()"/>
<INPUT type="text" ID="srt"  id="txtresult" NAME="result" VALUE="">
Ashish Patel
  • 772
  • 5
  • 12
1

Add below piece of code inside click=""

var e = document.getElementById('district');document.getElementById('txtresult').value= e.options[e.selectedIndex].value;

And there are two Id attribute in the bottom textbox, please remove id="Add".

Hope this helps.

Nitin Garg
  • 888
  • 6
  • 7
1

Try this: HTML ---

<h1>Please select a State</h1>

<select id="district">
<option value="Tiruvanandapuram">Kerala</option>
<option value="Chennai">Tamil Nadu</option>
<option value="New Delhi">Delhi</option>
</select>

<input type= "button" value= "show" id="btn" onclick= show_district() />
<input type="text" id = sss value = "" />

JavaScript :

function show_district() {
  var selector = document.getElementById('district');
  var value = selector[selector.selectedIndex].value;
  document.getElementById('sss').value = value;
}
Sumanta736
  • 695
  • 3
  • 10