Skip to content

Instantly share code, notes, and snippets.

@nastanford
Last active June 8, 2020 11:34
Show Gist options
  • Save nastanford/7471255 to your computer and use it in GitHub Desktop.
Save nastanford/7471255 to your computer and use it in GitHub Desktop.
Using JavaScript get the Text or Value of a Selected Item in a Select Box.
function getSelectedText(){
var e = document.getElementById("nameSelect");
var dspText = e.options[e.selectedIndex].text;
return dspText;
}
function getSelectedValue(){
var e = document.getElementById("nameSelect");
var dspValue = e.options[e.selectedIndex].value;
return dspValue;
}
<html>
<head>
<title>Test JavaScript</title>
</head>
<body>
<select id="nameSelect" onchange="example()">
<option value="1">Jim</option>
<option value="2">Tom</option>
<option value="3">Sally</option>
</select>
</body>
</html>
function example(){
var e = document.getElementById("nameSelect");
var dspText = e.options[e.selectedIndex].text;
var dspValue = e.options[e.selectedIndex].value;
alert(dspText+' = '+dspValue);
}
@ArneSaknussemm
Copy link

Thanks!

@nastanford
Copy link
Author

nastanford commented Jun 8, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment