Answers for "how to get value of option tag in javascript"

0

js get option value

<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>
</select>

var e = document.getElementById("ddlViewBy");
var strUser = e.value;
//Would make strUser be 2.

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;
//Would make strUser be test2.
Posted by: Guest on June-21-2021
0

get value from select tag

<form>
  Select your favorite fruit:
  <select id="mySelect">
    <option value="apple">Apple</option>
    <option value="orange">Orange</option>
    <option value="pineapple">Pineapple</option>
    <option value="banana">Banana</option>
  </select>
</form>

<script>
function myFunction() {
  var x = document.getElementById("mySelect").value;
  console.log(x);
}
</script>
Posted by: Guest on July-04-2021

Code answers related to "how to get value of option tag in javascript"

Browse Popular Code Answers by Language