-1

How to check what value is selected in dropdown html? in real time (client side)

<div>
    <select id="myList" name="testList">
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">Mar</option>
    </select>
</div>



 @{ 
    if ***[if the value from dropdown = 1) ]?***
      {
     var variable = x;
      }
 }
  • 4
    Does this answer your question? [Get selected value in dropdown list using JavaScript](https://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – Dominik Matis Jan 07 '20 at 11:19

4 Answers4

0

This might help: Get selected value in dropdown list using JavaScript

But here's an example:

getSelected();
function getSelected() {
 let list = document.getElementById("myList");
 let selected = list.options[list.selectedIndex].value;
 let writeSelected = document.getElementById("selectedItem")
  if (selected === "2") {
    return writeSelected.innerHTML = "Best month"
  }
  writeSelected.innerHTML = selected
}
<div>
    <select id="myList" onchange="getSelected()" name="testList">
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">Mar</option>
    </select>
</div>

<p>
Selected: <span id="selectedItem"></span>
</p>
Lumi
  • 439
  • 2
  • 8
0

I write sth this

<script>
       var XXX = document.getElementsByName("testList");
       var YYY = XXX.options[list_month.selectedIndex].value;

       var nr_days = YYY;
    </script>

but I have problem with variable: nr_days || comunicate: nr_days dont exist

  @for (int nr_rows = 0; nr_rows < nr_days; nr_rows++)

.cshtml

0

you can use this code to check what value is selected.

/***This code is to get value of selected option****/
$("select#myList").change(function(){
        var selectedMonth = $(this).children("option:selected").val();
        alert("You have selected the month - " + selectedMonth);
    });
/***This code is to get text of selected option****/
$("select#myList").change(function(){
        var selectedMonth = $(this).children("option:selected").text();
        alert("You have selected the month - " + selectedMonth);
    });
0
<html>
<head>
<script>
function demo()
{
var a=document.getElementById("txt").value;
document.write("selected month is"+a);
}
</script>
</head>
<body>
     <div>
    <select id="myList" name="testList" onchange="demo()" id="txt">
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">Mar</option>
    </select>
        </div>

</body>
</html>
  • Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Jan 07 '20 at 12:11