Last active
August 29, 2015 14:07
-
-
Save tiendq/a35e873418ba8d96e475 to your computer and use it in GitHub Desktop.
Get selected value/text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I saw a sample code like this, but don't do it. | |
function() { | |
var selectId = "mySelectList"; | |
try { | |
var options = document.getElementById(selectId).options; | |
for (var i = 0;i < options.length;i++){ | |
if(options[i].selected) { | |
return options[i].value; | |
} | |
} | |
} catch(e) {} | |
return ""; | |
} | |
// The much better way is using querySelector. | |
var selectItem = document.querySelector("#" + selectId); | |
return selectItem.options[selectItem.selectedIndex].value; | |
// And, finally, even better than better. | |
return document.getElementById(selectId).value; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment