-2

I have tried using the select object with JavaScript but it dosent seem to be going. do anyone know what am talking about

this is what I have tried

var x = document.getElementById("mySelect").selectedIndex;

var u = document.getElementsByTagName("option")[x].value;

        document.getElementById("demo").innerHTML=u;
gen_Eric
  • 223,194
  • 41
  • 299
  • 337

1 Answers1

3
var sel = document.getElementById("mySelect");  //reference the select
var index = sel.selectedIndex;  //find the index that was picked
var option = sel.options[index];  //select the option that was picked
document.getElementById("demo").innerHTML = option.value;  //read the value

if you want the text and not the value use .text

document.getElementById("demo").innerHTML = option.text;  //read the value

What this sample code does not do is deal if nothing is selected. That would be a selectedIndex equal to -1.

epascarello
  • 204,599
  • 20
  • 195
  • 236